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

123
Vistas
ReactJS Global Variable Not Being Changed From Fetch Method

I'm developing a ReactJS project that requires text to be read from an SVG file in the Public folder of the project directory, and then for global arrays to be populated with this information for other functions to utilize. I'm able to correctly read and parse out the data with a fetch method, but when I access the global variables after the function has ran, there is no change. However, when accessing the global variables from inside the method, the changes are there, but only within the scope of the fetch method, which is the main issue. How can I set a global variable to the results of a fetch method?

Here is the function: Note, that nodesArrayX and nodesArrayY are the global variables that I need to access and affect globally outside the scope of the fetch method.

function readNodeData() {
  var coordsDisplay = document.getElementById("current-coords");

  let testDataSet = [];

  fetch('/ccmNodetest.svg')
  .then(response => response.text())
  .then(data => {

    testDataSet = data.split("\n")
    init()

    //PARSE DATA
    var nodeDataLines = data.split("\n")
    var nodeDataStart = nodeDataLines.findIndex(element => element.includes("inkscape:label=\"NODES\""))
    nodeDataLines.splice(0,nodeDataStart)
    //coordsDisplay.innerText = nodeDataLines[0]

    var nodeDataParseCount = 1
    while (nodeDataLines[nodeDataParseCount] != "  </g>") {
      nodeDataParseCount += 3
      var testingNodesData = nodeDataLines[nodeDataParseCount]
      testingNodesData = testingNodesData.substring(11)
      testingNodesData = testingNodesData.slice(0,-1)
      nodesArrayX.push(testingNodesData)

      nodeDataParseCount += 1
      testingNodesData = nodeDataLines[nodeDataParseCount]
      testingNodesData = testingNodesData.substring(11)
      testingNodesData = testingNodesData.slice(0,-1)
      nodesArrayY.push(testingNodesData)

      nodeDataParseCount += 2
    }
    //PARSE DATA
  });

  function init() {
    console.log(testDataSet);
  }

}

Overall, the main issue is I can use a fetch method to access the file from the Public folder, read it, parse it out and push the new entries to the global arrays stated above. However, after the function has ran, the changes are not present, and are only present when calling the variables inside the scope of the fetch method. Any suggestions would be greatly appreciated, been stuck on this for a long time. Thank you!

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