Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

160
Vistas
cant set a state inside function (react)

Learning by coding, here i have simple graph, where are different datas such as current, all and filtered data, first code works fine, but i wanted to setState so i changed code a little (second code), if i uncomment 'setTest' it give 'Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.' is there a fix to this or should i somehow use useEffect? and another question is why should i call function 'kk()', and not just reference to it 'kk' because when i use reference it give error 'Uncaught TypeError: Cannot read properties of undefined' but when calling function it wont give error, but then i have used reference to function without problem 'onClickNode={onClickNode}' .

1:

import { Graph } from "react-d3-graph";
<Graph data={ 
hideData ? 
curentData ? allData : filteredData 
: allData } 
      onClickNode={onClickNode}


/>

2:

 import { Graph } from "react-d3-graph";
 const [test, setTest] = useState<any>("testData1");
 
  const kk = () => {
   // setTest("testData2")
    return curentData ? allData : filteredData;
  };

 
 <Graph
          data={hideData ? kk() : allData}   onClickNode={onClickNode}

        />

English is not my mother language so could be mistakes.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You should not call a state changing function within your rendering logic without some sort of condition. Without a condition to prevent a state change results in an infinite loop as you are seeing.

One option you can try is call setTest("testData2") on a particular user action, like this:

  const [test, setTest] = useState<any>("testData1");
 
  const kk = () => {
    return curentData ? allData : filteredData;
  };
  
  const onClickNode = () => {
    // Handle other click logic...
    setTest("testData2");
  };
  
  <Graph data={hideData ? kk() : allData} onClickNode={onClickNode} />

But without seeing more of that code, that option may not make sense for you.

Another option would be to check the value of test before calling setTest("testData2"), like this:

  const [test, setTest] = useState<any>("testData1");
  
  const kk = () => {
    if (test !== "testData2") {
      setTest("testData2");
    }
    return curentData ? allData : filteredData;
  };
 
  <Graph data={hideData ? kk() : allData} onClickNode={onClickNode} />
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda