frankcollins3 / PHPokedex

react concepts
0 stars 0 forks source link

redux store() variable definition and scope [12:55pm] #40

Open frankcollins3 opened 1 year ago

frankcollins3 commented 1 year ago

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

frankcollins3 commented 1 year ago

Screen Shot 2023-05-12 at 12 56 05 AM

here is the error one may encounter upon invoking setState() without asynchronously awaiting the returned fetch data

[12:56pm]

frankcollins3 commented 1 year ago
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]