Redux Persist Gate的React + Rails

时间:2019-03-21 03:08:50

标签: ruby-on-rails reactjs react-redux redux-persist

对于React_on_rails来说,我是所有事物的新手,我试图弄清楚如何在项目中使用redux-persist,以便在更改页面时不会丢失任何redux存储。我已经确定了redux设置,但是我无法让Redux Persist正常工作,但是它仍然无法Uncaught Error: Could not find store registered with name 'rootStore'. Registered store names include [ ]. Maybe you forgot to register the store?,我只是想知道是否有人可以帮助您解决问题。我试图翻阅文档多次,并确实为我提供了选择的帮助。

我的应用程序视图

<!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>

我注册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;

最后一个文件将处理以后将注入的所有其他组件。

// @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>;
  }
}

1 个答案:

答案 0 :(得分:0)

经过几个小时的调试,我实际上发现了为什么我的persistStore无法正常工作。这是我在代码中所做的事情。

store / rootStore

_persistor = persistReducer(_store); => _persistor = persistStore(_store);

主条目文件

ReactOnRails.register({ProductDetailPage});
registerRootStore();

应该是

registerRootStore();
getReactOnRails().register({ProductDetailPage});

最后是一个响应PersistGate和redux Provider的组件,应将其呈现在组件中,而不是在类外部,并且应像这样

const rootStore = getReactOnRails.getStore('rootStore');