Open frankcollins3 opened 1 year ago
here is the error one may encounter upon invoking setState() without asynchronously awaiting the returned fetch data
[12:56pm]
import '../../App.css';
import {useEffect, useState} from 'react'
import store from "../../redux/store"
import actionObject from "../../redux/actions.js"
const firefunc = () => { return }
const firefunc2 = () => { return }
const apitest = () => { return }
function RealScreen () {
let flexclasscolumn = ["flex", "column", "justCenterAlignCenter"].join(" ");
let flexclassrow = ["flex", "row", "justCenterAlignCenter", "Pokeball-Container"].join(" ")
const AppClass = ["App", "flex", "column", "justCenterAlignCenter"].join(" ");
let globalvar
let pokemon;
const getstate = async () => {
let slowpoke = await actionObject.slowpoke()
let predata = await fetch(`http://localhost:5000/pokemon?query={allAPIpokemon{name,poke_id}}`)
// let predata = await fetch(`http://localhost:5000/pokemon?query={allDBpokemon{name,poke_id}}`)
const allAPIpokemon = await predata.json()
const allpokemon = allAPIpokemon.data.allAPIpokemon
console.log('store')
console.log(store)
console.log('pokemon')
console.log(pokemon)
allpokemon.forEach(async(pokemon) => {
let pokename = await actionObject.setPokemon(pokemon.name).payload
console.log('pokename')
console.log(pokename)
})
}
useEffect( () => {
(async() => {
globalvar = await store.getState();
pokemon = store.pokemon
})()
}, [])
useEffect( () => {
(async() => {
globalvar = await store.getState();
pokemon = store.pokemon
})()
👍 globalvar = await store.getState()
👎 store = await store.getState() doing it this way didn't work.
import store from "./store" let store;
the declaring of the word store that was the same word as the important seemed to intrude upon each other.
calling store a variable name other than store, in this case globalvar allowed it to have access to the getState() props
[1:03pm]
store.getState() needs to be invoked asynchronously but now that variable will be out of scope from the rest of the app if it were within an async function