I have an array that I get from a hook - the playlists
and a trying to put it in the state, in the playlistsLocal
, but it seems that my state variable is still empty when I view it in the console. I know that setting the state is asynchronous and that kind of stuff but it is really that challenging to set the state?
Sorry maybe I am doing something wrong can you guys have a look please
import React, { useState, useEffect } from 'react'
import { useStoreState, useStoreActions } from 'easy-peasy'
import { Select } from 'antd'
function PlaylistSelector() {
const playlists = useStoreState(state => state.playlists.items)
const reloadPlaylists = useStoreActions(actions => actions.playlists.reload)
const [playlistsLocal, setPlaylistsLocal] = useState(null)
useEffect(() => {
reloadPlaylists()
setPlaylistsLocal(playlists);
}, [playlistsLocal])
var el = <Select style={{ width: '100%' }} defaultValue={playlistsLocal[0]?.name}>
{playlists.map((e, key) => {
return <option key={key} value={e.value}>{e.name}</option>;
})}
</Select>
console.log('playlistsLocal ', playlistsLocal);
return el
}
export default PlaylistSelector