shakacode / reactrails-react-native-client

This repository is for a react-native client to the https://www.reactrails.com/, source at https://github.com/shakacode/react-webpack-rails-tutorial/.
http://www.shakacode.com/work
30 stars 7 forks source link

Explain how initialState is used? #5

Closed badnorseman closed 8 years ago

badnorseman commented 8 years ago

Someone interested to walk me through this and explain it?

import reducers, { initialStates } from '../reducers';

export default () => {
  const { $$commentsState } = initialStates;
  const initialState = {
    $$commentsStore: $$commentsState.merge({
      $$comments: [],
    }),
  };

  const reducer = combineReducers(reducers);
  const composedStore = compose(
    applyMiddleware(thunkMiddleware, loggerMiddleware)
  );

  return composedStore(createStore)(reducer, initialState);
};
justin808 commented 8 years ago

@alleycat-at-git, it's worth commenting in the code that for a simple app, we really didn't need a bunch of this code, as this is geared for the case when we have many reducers in a bigger, real production app. Since we're making this setup work for real production apps, then this code is correct.

Anyway, this is symetrical to this code: https://github.com/shakacode/react-webpack-rails-tutorial/blob/master/client/app/bundles/comments/store/commentsStore.js#L8

Another cool thing (maybe an issue for later) would be to see how we could re-use the non-component code in both web and native react projects. Maybe having one top level github project is the way to go that?

CC: @robwise

robwise commented 8 years ago

This is where we create the initial state for the entire store, which is a composition of each reducer's initial state mapped to that reducer's name in the store. This seems to be using the old createStore syntax though.

since you're not merging anything from the initial props, I think you can just do:

import reducers, { initialStates } from '../reducers';

export default () => {
  const rootReducer = combineReducers(reducers);

  return createStore(rootReducer, initialStates, compose(applyMiddleware(thunkMiddleware, loggerMiddleware));
};
alexeuler commented 8 years ago

@robwise Agreed, since we won't have server rendering here, we can make it simpler