を反応させるために状態を初期化するに置く必要があります。キーは「ユーザー」があるとアプリが開かれた反応時に私が欲しいが、それはReduxのツリーを作成し、値がユーザーオブジェクトがのlocalStorageから解析されている(ときに、ユーザログされる)。Reduxの:私たちは、たとえばアプリ
createStore
に入れるべきだと思います。だから、ストアが作成されるように、右のように、私はオブジェクトを初期化
import { applyMiddleware, compose, createStore } from 'redux';
import thunk from 'redux-thunk';
import { browserHistory } from 'react-router';
import { syncHistoryWithStore } from 'react-router-redux';
import makeRootReducer from './reducers';
import { updateLocation } from './location';
export default (initialState = {}) => {
const middleware = [thunk];
const enhancers = [];
let composeEnhancers = compose;
const store = createStore(
makeRootReducer(),
initialState,
composeEnhancers(applyMiddleware(...middleware), ...enhancers)
);
window.store = store;
store.asyncReducers = {};
store.unsubscribeHistory = browserHistory.listen(updateLocation(store));
// const history = syncHistoryWithStore(browserHistory, store);
// console.log(history);
return { store: store, history: null };
};
:ここストアを作成するための私のコードがある
const store = createStore(
makeRootReducer(),
initialState,
composeEnhancers(applyMiddleware(...middleware), ...enhancers)
);
store.getState().sampleString = "StackOverFlow";
しかし、私は、アプリが起動される反応したとき、私は「sampleString」を参照してくださいしないでくださいこの還元木のキー。私が間違っていた部分を見つけ出すのを助けてください。
おかげ
あなたが状態を変更するためのいくつかのアクションをディスパッチする必要があります。私は、それが状態変化をトリガーする唯一の方法だと思う。 –
あなたはその関数に渡すinitialStateがどこに定義されていますか?その変数はあなたの鍵を保持しているはずです。状態を最初のものにしたい場合は、状態を変更するべきではありません。 – lilezek
@EQuimperなぜredux-persistが私の問題を解決できるかについてもっと詳しく説明できますか?私が知っていたように、redux-persistは還元ツリーをローカルストレージに保存するのに役立ちます。 –