Search code examples
reactjsreduxreact-hooksreact-reduxredux-thunk

How to pass data from useSelector (redux) to useSate hook in react?


Hi i am trying to work on a app, which has some predefined code of redux, and react. I am trying to get the players data which is formated json file and pass it to useSate hook.

The data which I get using

 const players = useSelector(getPlayers); // redux 

looks perfect when I do console log, however when I pass this to useState

const [playerData, setPlayerData] = useState(players); //react

there is no data on the console.

I do not know if this is the right way to do, if not what would be the best solution to this? as I am more into react hooks and not redux.

any help is appreciated . Thanks

enter image description here


Solution

  • you can set it in useEffect like that but my suggestion is to use the directly redux players variable.

    useEffect(() => {
      setPlayerData(players);
    }, [players])