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

245
Visualizações
Updating component state inside array.map() causing too many re-renders
import useState from "react-usestateref";

const [meta, setMeta, metaRef] = useState({});

Inside component's JSX:

data.result.map((token) => {
                        const id = token.token_id;
                        const params = { uri: token.token_uri };

                        if (token.metadata) {
                            setMeta(JSON.parse(token.metadata));
                        } else {
                            Moralis.Cloud.run("get_token_uri", params).then(
                                (response) => setMeta(response)
                            );
                        }
                        const { name, description, imageUrl } = metaRef.current;
                        return (
                            <Card
                                key={id}
                                tokenId={id}
                                name={name}
                                description={description}
                                user_address={user_address}
                                imageUrl={fixURL(imageUrl)}
                                calledFrom={calledFrom}
                            />
                        );
                    })

I want to update the meta state variable from inside data.result.map and use the updated meta object to return a Card component. I'm using react-usestateref to get the current value of the meta state variable. Otherwise, meta still remains {} when de-structuring, even after being updated. If token.metadata is not null I want to set the meta state var to token.metadata obj and if it is null, I want to make an asynchronous request to get back the object from Moralis and then set it to the meta state var. This code is producing the following error: Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.

How can I make this code work as intended?

about 4 years ago · Juan Pablo Isaza
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