【问题标题】:Initialize state with async data in Redux在 Redux 中使用异步数据初始化状态
【发布时间】:2017-01-22 00:46:29
【问题描述】:

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

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

有没有在 createStore 被调用之前运行的函数?

【问题讨论】:

  • 您想在创建商店时进行 ajax 调用并在渲染之前填充这两个字段?这是在服务器端渲染上下文中还是在客户端上?

标签: reactjs react-native redux react-redux


【解决方案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;
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-29
    • 2016-09-22
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 2016-09-04
    • 1970-01-01
    • 2020-02-22
    相关资源
    最近更新 更多