How to connect Rails and Redux with React on Rails

Our project is using gem ‘react on rails’. This gem has a method for Redux, so I ‘m going to summarize it in order not to forget it.

By using this method, you can give an object from controller to store like this.

There are hogeReducer, hogeStore in this project.

include ReactOnRails::Controllerdef index
redux_store('hogeStore', props: { hoge: hogeId: @hoge_id } })

Before that there are two tasks you have to do.

The first one is,

You have to register the store to React on Rails.

ReactOnRails.registerStore({  hogeStore,});

The second one is,

You have to use getStore for React on Rails, when you make a store.

const store = ReactOnRails.getStore('hogeStore');
export default (props: any) => {  const { hogeId } = props;  const initialState = { hogeId: hogeId };  const reducer = combineReducers(reducers);  const store = createStore(    reducer,    initialState,  );  return store;};

You can get it from props directly!