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

131
Visualizações
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 à 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