i have an object format api like this:
{ success: 1,
result:[
{},
{},
{},
{} ]
}
how can i named a map on result parameter of this object? :/
i try 2 ways for it:
1- define api to a variable like x: const [x, setX] = useState([]);
then define result of api to another varibale like: const y = x.result;
and then make a map on Y .
2- define api to a variable like x: const [x, setX] = useState([]);
then make it array with this method: const y = Object.entries(x);
and get "result" of this array like this:
const result = y[1];
and make a map on result .
but after every 2 ways i see this error in console: :(
TypeError: Cannot read properties of undefined (reading 'map')
I found that you use useEffect a little bit incorrectly. You don't need return result in useEffect function. Instead it should be passed to setState function as argument:
useEffect(async () => {
const players = await getPlayers();
setPlayers(players);
}, []);
as your player data fetch operation is asynchronous, you can use conditional rendering in component:
{result ? result.map(player => <Player key={player.player_key} data={player} />) : loading...}
to shure that result won't be undefined at moment of component mounting.