Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there way to get return value from reducer action? (in React-Native)

I'm making an app using React-Native.

I have a data array in redux state. I would say it's User's array.

I want to pick one user and get his data directly from reducer state.

But I can't get a return value from reducer action. And accessing a store and use getState() doesn't seem a good way. Firstly, it's too hard to access 'store' instance located in root component.

What options do I have?

  • Note : I already know I can add another state like 'pickedUser' and set it by action. But I want to know if there is another way to get state directly.

I'm looking for something like calling an action and get the return value.

like image 665
Bright Lee Avatar asked Mar 08 '23 04:03

Bright Lee


1 Answers

I want to pick one user and get his data directly from reducer state.

There is nothing like a reducer state. A reducer is a pure function that takes the previous state and an action, and returns the next state.

It should never be called by yourself but by redux to update the state. You can listen for state changes by connecting e.g. your react components to the store with the connect() function from the react-redux package. If a part of the state that your components is listening to updates it will trigger a re-render of that connected component by passing the updated state as props. This is the way to react to state changes.

I'm looking for something like calling an action and get the return value.

Redux actions do not return anything. They just trigger changes of the global state which you can then listen to. If you need to e.g. fetch data (to update the state with) inside an action you need some kind of async action which e.g. redux-thunk provides.

like image 114
trixn Avatar answered May 20 '23 21:05

trixn