How to connect Rails and Redux with React on Rails

Tomoharu Tsutsumi
1 min readJul 12, 2020

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.

The main method, ‘redux_store’

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 } })
end

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');

How to get the object from the controller in the store

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!

Started LinkedIn as well!

https://www.linkedin.com/in/tomoharu-tsutsumi-56051a126/

--

--

Tomoharu Tsutsumi
Tomoharu Tsutsumi

Written by Tomoharu Tsutsumi

5+ years Full Stack SWE (Ruby, Go, TypeScript, JavaScript) | Former Founding Engineer of AI Startup in Canada

No responses yet