I am struggling with a react promise issue. I think I need to be able to use a state length in a for loop outside the axios .then function. Presently, the code below works when I replace LEN with a numerical value but will not work if I try to use label.length as that value does not exist when the for loop is called. Could anyone advise on the best way to handle this? My code is crude as I'm new to js and react.
export default function App() {
const [label, setLabel] = useState([]);
const handle = useFullScreenHandle();
const [data, setData] = useState([]);
const token = fetch('token');
const headers = {
'X-CSRF-Token': token,
'Authorization': 'Basic '+btoa('cred')
}
React.useEffect(() => {
axios
.get("url", {
headers: headers
})
.then(result => {
setLabel(Object.keys(result.data).map(key => result.data[key].title));
setData(Object.keys(result.data).map(key => result.data[key].nid));
len=label.length
});
}, []);
console.log(length)
for (let i = 0; i < **LEN**; i++) {
i = Number(i)
lab = label[i];
dat = data[i];
var stri = ({id:String(i), category: 'cat', label:"String(i)"+i});
//JSON.stringify({artist: artist, title : title});
nodes.push (stri);
var ed = ({id:String(i),source:String(i),target:String(i+1),label:i});
edges.push(ed);
}
var datada = ({nodes: nodes, edges:edges});
let element =
<>
<Sigma graph={datada}></Sigma>
return element
</>
}