Home > Net >  React: State is undefined
React: State is undefined

Time:02-04

** There is currently wrong something with my code, can anyone help?**

  const {data: coinsList, isFetching} = useGetCoinsQuery();
  const coinsData = coinsList?.data?.coins
  console.log(coinsData)

  When I console.log() the data I can see it in the browser but when I put it in a state like 
  this: const [coins, setCoins] = useState(coinsData) console.log(coins), I cant 
  see the data, it returns undefined, can someone explain whats wrong
  

CodePudding user response:

I also tried to set the state after:

const [coins, setCoins] = useState([])
setCoins(coinsData)

but it still doesn't work

CodePudding user response:

When Coinlist is change then useEffect hook is called and save the value 
coins.


const {data: coinsList, isFetching} = useGetCoinsQuery();
const coinsData = coinsList?.data?.coins
console.log(coinsData)
const [coins, setCoins] = useState([])

useEffect(()=>{
   setCoins(coinsData)
 },[coinsData])
  •  Tags:  
  • Related