Redux中的初始状态出现问题并做出本机反应

时间:2018-10-08 20:06:41

标签: reactjs react-native redux

我是react-native的新手,想知道window.___INTITIAL_STATE__的工作。

main.js

import React from "react";
import createStore from "./store/createStore";
import AppContainer from "./AppContainer";

export default class Root extends React.Component {
    renderApp() {
        const initialState = window.___INTITIAL_STATE__;
        const store = createStore(initialState);
        return <AppContainer store={store} />;
    }
    render() {
        return this.renderApp();
    }
}

createStore.js

import { createStore, applyMiddleware, compose } from "redux";
import thunk from "redux-thunk";
import makeRootReducer from "./reducers";
import { createLogger } from "redux-logger";

const log = createLogger({ diff: true, collapsed: true });

export default (initialState = {}) => {
    const middleware = [thunk, log];
    const enhancers = [];
    const store = createStore(
           makeRootReducer(),
           initialState,
           compose(
                 applyMiddleware(...middleware),
                 ...enhancers
           )
    );
    return store;
};

0 个答案:

没有答案