ShakaCode | ShakaCode Blog | Rails On Maui Blog | Rails | ReactJs | JavaScript | Webpack | Productivity |

React + Rails with Redux Persist Gate


#1

I am new to everything as specially to react_on_rails and I am trying to figure out how to use redux-persist with my project so that when I changing the page, I am not losing any redux store. I have figure out the redux setup but I cannot get Redux Persist to work right and it still fails to Uncaught Error: Could not find store registered with name 'rootStore'. Registered store names include [ ]. Maybe you forgot to register the store? I just wondering if anyone can help with it to fix a problem. I tried to go over the documentation several times and did really helped me with the option that I have.

my application view

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
  <%= csrf_meta_tags %>
  <%= csp_meta_tag %>

  <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>

<body>
<%= notice %>
<%= alert %>
    <%= redux_store('rootStore', props: {}) %>
    <%= react_component('ProductDetailPage', props: {product: @product.id}) %>
    <%=  yield %>
    <%= redux_store_hydration_data %>
</body>
</html>

My entry point to register ProductDetailPage

import ReactOnRails from 'react-on-rails';
import ProductDetailPage from '../pages/ProductDetailPage';
import { registerRootStore } from '../utils/ReactOnRails';
ReactOnRails.setOptions({
  traceTurbolinks: true,
});
ReactOnRails.register({ProductDetailPage});
registerRootStore();

utils/ReactOnRails

import { configureStore } from '../store/rootStore';
export default function getReactOnRails() {
  window.ReactOnRails = window.ReactOnRails || require('react-on-rails').default;
  return window.ReactOnRails;
}

export const registerRootStore = function () {
  if (getReactOnRails().storeGenerators().has('rootStore')) {
    return;
  }
  getReactOnRails().registerStore({rootStore: configureStore });
};

store/rootStore

import { createStore } from 'redux';
import reducerIndex from '../reducers/index';
import { persistReducer} from 'redux-persist';

let _store;
let _persistor;
export const configureStore = function (props) {
  if (_store) return _store;

  const initialState = (props) ? {...props} : {};
  _store = createStore(reducerIndex, initialState);
  _persistor = persistReducer(_store);

  return _store;
};

export const getPersistor = function () {
  return _persistor;
};

reducers/index

import { combineReducers } from 'redux';
import { persistReducer} from 'redux-persist';
import cartReducer from './cartReducer';

const rootReducer = combineReducers({
  cart: cartReducer,
});

const reducer = persistReducer(
  {
    key: 'root',
    storage: require('localforage'),
  },
  rootReducer
);

export default reducer;

And the last file which handles all other components that will be later injected with.

// @flow

import * as React from 'react';
import { Provider } from 'react-redux';
import getReactOnRails from '../utils/ReactOnRails';
import { PersistGate } from 'redux-persist/es/integration/react';
import { getPersistor } from '../store/rootStore';

type Props = {
  children: React.Node,
  loading?: React.Node,
}
const rootStore = getReactOnRails.getStore('rootStore'); // another error that happening for me, it says that getStore is not a function.

export default class ProviderGate extends React.Component<Props> {
  render() {
    const persistor = getPersistor();

    if (!persistor) return this.props.children;

    return <Provider store={rootStore}>
      <PersistGate persistor={persistor} loading={this.props.loading}>
        {this.props.children}
      </PersistGate>
    </Provider>;
  }
}