Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
React context states not updating / reverting to default

Hi so I made a context for providing Web3 details globally to make everything easier and in sync. It was working great and then something happened and suddenly the states for the context refuse to change.

This is the context

export const Web3Context = React.createContext<IWeb3Context>({
  contractAddress: CONTRACT_ADDRESS,
  provider: providerTemp,
  setProvider: () => {},
  signer: undefined,
  setSigner: () => {},
  contract: undefined,
  updateContract: () => {},
  account: 'Not Connected',
  updateAccount: () => {},
  connected: false,
  setConnected: () => {},
});

Context function

const Web3Provider: React.FC<Web3ProviderProps> = ({ children }) => {
  const [provider, setProvider] = useState(providerTemp);
  const [signer, setSigner] = useState(undefined);
  const [contract, setContract] = useState(
    new ethers.Contract(CONTRACT_ADDRESS, Chese.abi, providerTemp),
  );
  const [account, setAccount] = useState('Not Connected');
  const updateAccount = (accountTemp: string) => {
    setAccount(accountTemp);
  };
  const [connected, setConnected] = useState(false);
  const updateContract = (signer: ethers.providers.JsonRpcSigner) => {
    const yeah = new ethers.Contract(CONTRACT_ADDRESS, Chese.abi, signer);
    setContract(yeah);
  };
  const value = {
    CONTRACT_ADDRESS,
    provider,
    setProvider,
    signer,
    setSigner,
    contract,
    updateContract,
    account,
    updateAccount,
    connected,
    setConnected,
  };
  return <Web3Context.Provider value={value}>{children}</Web3Context.Provider>;
};

I believe something is causing rerenders somewhere and that is forcing everything back to defaults? In my navigation there is a "Connect Wallet" button that becomes an "Account" link when the connect state is true. When you click connect wallet it updates the whole context with multiple state updates one after another. And then the button switches to a Gatsby.js Link component to bring user to a new page.

For some reason, clicking connect wallet will connect the wallet with metamask and it does change the button into the account link which means that connect was set to true and rerendered the navigation and it stays that way until refresh.

{Web3Context.connected ? (
  <Link
    to="/account"
      className="text-white bg-gradient hover:text-white hover:bg-black hover:bg-none rounded-2xl px-4 py-3 my-0"
  >
   My Account
  </Link>
) : (
  <button
    onClick={connectWallet}
      className="text-white bg-gradient hover:text-white hover:bg-black hover:bg-none rounded-2xl px-4 py-3 my-0"
  >
    Connect Wallet
  </button>
)}

When I try to update any of the states that the context uses it seemingly fails and everything is default values.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Figured it out.

The onClick in my button was onClick={connectWallet} which was causing lots of rerenders and forcing the context back to default. Switched it to onClick={() => connectWallet()}

I am using Gatsby. I also wasn't wrapping the context provider correctly which was resetting the context between internal links. As per Gatsby docs.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda