私はreduxを勉強しています。どのように状態をローカルに保存できるのか知りたがっています。私はredux-persistについて知りに来て、githubの医者にそれを使う方法を説明しましたが、それは私にはあまり明確ではありませんでした。redux-persist:ブラウザのローカルストレージに状態を保存する方法は?
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import App from './components/app';
import reducers from './reducers';
const createStoreWithMiddleware = applyMiddleware()(createStore);
ReactDOM.render(
<Provider store={createStoreWithMiddleware(reducers)}>
<App />
</Provider>
, document.querySelector('#container'));
どのように私はそれを達成することができます - ここで
は私のアプリのindex.jsコードはありますか?
import React from 'react';
import { Provider } from 'react-redux';
import { browserHistory } from 'react-router';
import { syncHistoryWithStore } from 'react-router-redux';
import createRoutes from './routes'; // Contains the routes
import { initStore, persistReduxStore } from './store';
import { appExample } from './container/reducers';
const store = initStore(appExample);
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = { rehydrated: false };
}
componentWillMount() {
persistReduxStore(store)(() => this.setState({ rehydrated: true }));
}
render() {
const history = syncHistoryWithStore(browserHistory, store);
return (
<Provider store={store}>
{createRoutes(history)}
</Provider>
);
}
}
このコード:、あなたの店この方法を作成し、あなたのapp.jsで
:ここ –
'ログイン 'は必須ではなく、単に認証の問題を処理するためのものです。あなたのケースのコードでそれを削除するのを忘れました – yuantonito