react-redux icon indicating copy to clipboard operation
react-redux copied to clipboard

Accessing Store inside Reducer

Open NoelBroda opened this issue 9 years ago • 3 comments

Hi guys! First of all, sorry for my bad english.

I have a question/proposal. But, you have to know I have less than 1 month working with React and Redux... so... Maybe I'm absolutely crazy.

I have an APP that the 90% of the APP is related to show analytics information about people. So, I have 2 keys in my store: analytics and people.

people is an array that is being updated every 100ms... I have kind of stream for my backend, fulling store.people array.

Every time the user click on Update Analytics Data in the VIEW, I call the UPDATE_ANALYTICS_DATA action and I have to analyze all people array and get some information (avg age, avg children, etc)

When I was doing it, I found a big issue: I can not access to people in analytics reducer. So, people recommended me to use thunks or selectors or sending the people array as a action data.

Here, the different problems:

  • Resolving using Thunks. I don't like it. Absolutely. Accessing to the dispatch function in a action creator, is (in my point of view) something against the Redux pattern. View should call dispatchers. Only view.
  • Resolving sending people as a Action data. I can't. I can not map people to my view, because people is update every 100ms. I will generate extras renders.
  • Selectors. I used it to fix my problem. So, now, I have a selector to create my analytics data, so now, analytics is not in the store :( but, the theory say that store should contains all the information to "describe" the app-status. So Analytics should be inside my store.

So, I thought: How can it be fixed?

  • Acceptance Criteria 1: Only call dispatch in views.
  • Acceptance Criteria 2: Do not access to the Store in action creators.
  • Acceptance Criteria 3: Avoid using creators. All data should be created in reducers and all data should be save inside store.

So, I thought: Why don't we receive a clone of the Store as a third parameter in all reducers?

If I received the store as a third parameter, I could have a analytics reducer, call the UPDATE_ANALYTICS_DATA in the view (with no necessity to send any more data) and just calculate it in the reducer.

Make it sense? Is it absolutely crazy? Do you recommend to access to people in the actionCreator, using the second parameter of "mapDispatchToProps"?

NoelBroda avatar May 29 '17 17:05 NoelBroda

Oh! I asked using my company account. This is my personal account.

BrodaNoel avatar May 29 '17 17:05 BrodaNoel

Sounds like you're using combineReducers. You should read about moving beyond that particular API: http://redux.js.org/docs/recipes/reducers/BeyondCombineReducers.html

timdorr avatar May 29 '17 18:05 timdorr

Yeap, you're right. I'll take a look. Thanks.

NoelBroda avatar May 29 '17 19:05 NoelBroda