使用Redux中的异步数据初始化状态

时间:2017-01-22 00:46:30

标签: reactjs react-native redux react-redux

每当创建状态时,我想通过AJAX填充两个标记属性。似乎Redux没有太多关于此的文档。我不能使用componentWillMount来执行此操作,因为我的容器设置它的方式不起作用。

const Auth = Record({
  token: '',
  yelpToken: '',
});

有没有运行在调用createStore之前会发生的函数?

1 个答案:

答案 0 :(得分:1)

您可以使用以下代码替换索引:

 class EntryPoint extends Components {
        constructor(){
            this.state = {
                //can be replaced with store state..
                finishedFetching: false
            }
        }
        componentDidMount() {
            //you can chain dispatches with redux thunk
            dispatch(fetchAsyncData())
                .then(() => this.setState({finishedFetching: true}))
        }
        render() {
            return this.state.finishedFetching ? <App/> : null;
        }
}