0

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

Tapesh Patel
  • 131
  • 1
  • 17
  • Hi, I would suggest you to use the `players` variable itself and not set another state inside your component. If you think about it, if the state was to be maintained in the component, then why would it be declared in the redux, right ? So instead of creating a new state, just use what is stored in redux. – Tirth Trivedi Jul 13 '22 at 12:22
  • Hi @TirthTrivedi, Thanks for the help, but when I try to destructure I get undefined, do you know why is it so ? – Tapesh Patel Jul 13 '22 at 12:32
  • getPlayers is a method? I need to see the redux structure. In useSelector, basically you have to do something like this `const players = useSelector((state) => state.playes)`. – Tirth Trivedi Jul 13 '22 at 12:34
  • Sure if you are a free for a remote session I can send you google meet link show the whole code please let me know @TirthTrivedi – Tapesh Patel Jul 13 '22 at 12:37
  • Just send me the redux state structure if possible. A screenshot of state from redux devtools will also work. Can't commit for the remote session as of now :) – Tirth Trivedi Jul 13 '22 at 12:39
  • Hi @TirthTrivedi I have updated the code with dev tool screenshot – Tapesh Patel Jul 13 '22 at 12:43
  • Okay, just use this code snipper `const players = useSelector((state) => state.players); console.log(players);` – Tirth Trivedi Jul 13 '22 at 12:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/246403/discussion-between-tapesh-patel-and-tirth-trivedi). – Tapesh Patel Jul 13 '22 at 13:00
  • here can u pls share your reducer code. – Kanti vekariya Jul 13 '22 at 13:16
  • could you please help me with this question if possible? https://stackoverflow.com/questions/72967585/how-delete-an-item-using-redux-dispatch-and-react @TirthTrivedi – Tapesh Patel Jul 13 '22 at 14:11
  • Do you intend to mutate the local `players` state only in this component but *not* in redux? It's not clear from the question *why* you want to pass data from a selector to local state. Also when is `players/fetchAll` being dispatched? Is the data available when this component is mounted? – timotgl Jul 14 '22 at 11:30
  • Hi @timotgl Thanks for the help but I figured out the solution and mistake I was doing. Really appreciated your response sir. – Tapesh Patel Jul 14 '22 at 11:46
  • 1
    @TapeshPatel Consider updating the question with this info (and what the solution was) so visitors know there is no more help needed. – timotgl Jul 14 '22 at 12:40

1 Answers1

1

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

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

Kanti vekariya
  • 667
  • 3
  • 13