I have a function fetching data through Axios in the zustand store:
import create from 'zustand'
import axios from "axios";
export const useStore = create((set) => ({
pokemons: [],
eachPokemon: {},
getPokemons: async () => {
const response = await axios.get('https://pokeapi.co/api/v2/pokemon')
set({ pokemons: await response.data.results })
},
getPokemon: async(pokemonURL) => {
const response = await axios.get(pokemonURL)
set({ eachPokemon: await response.data})
}
}))
The functions called in the useEffect hooks:
import React, {useEffect} from 'react'
import { useStore } from '../../store/store'
import ItemDetails from './ItemDetails'
function Items() {
const {pokemons, getPokemons} = useStore((state) => ({
pokemons: state.pokemons,
getPokemons: state.getPokemons
}))
useEffect(() => {
getPokemons();
console.log(pokemons)
}, [getPokemons])
return (
<>
Some code
</>
)
}
export default Items
The console.log in the 'Items' functional component's useEffect hook gets called twice.
Furthermore, when placing the console.log outside the useEffect hook, the data is returned multiple times.
import React, {useEffect} from 'react'
import { useStore } from '../../store/store'
import ItemDetails from './ItemDetails'
function Items() {
const {pokemons, getPokemons} = useStore((state) => ({
pokemons: state.pokemons,
getPokemons: state.getPokemons
}))
useEffect(() => {
getPokemons();
}, [getPokemons])
console.log(pokemons)
return (
<>
Some code
</>
)
}
export default Items
Edit: Here is what I see in my dev tools console for the code written above =
https://reactjs.org/link/react-devtools
Items.jsx:16 []
Items.jsx:16 (20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Items.jsx:16 (20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Items.jsx:16 (20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Items.jsx:16 (20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Items.jsx:16 (20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Items.jsx:16 (20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Items.jsx:16 (20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Items.jsx:16 (20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Items.jsx:16 (20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Items.jsx:16 (20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Items.jsx:16 (20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Items.jsx:16 (20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Items.jsx:16 (20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Items.jsx:16 (20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Items.jsx:16 (20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Items.jsx:16 (20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Items.jsx:16 (20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Items.jsx:16 (20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Items.jsx:16 (20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Items.jsx:16 (20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Items.jsx:16 (20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
Why is this happening and how can I receive the data once?