【问题标题】:How to insure that all redux states are loaded before react component rendered?如何确保在渲染反应组件之前加载所有 redux 状态?
【发布时间】:2018-04-08 07:00:29
【问题描述】:

当用户登录时,我正在使用用户的 fk_branch_id 进入 redux 存储。

我正在 react componentDidMount 中初始化 jquery 数据表。 并在数据表初始化中,我发送带有 ajax 标头的 fk_branch_id

beforeSend: (request) => {
  let branchId = _.isEmpty(this.props.active_branch)?null:this.props.active_branch.branch_id;
  request.setRequestHeader("fk_branch_id", branchId);
},

但是当发送 ajax 时,fk_branch_id 为 null,因为尚未加载来自 redux 状态的 fk_branch_id,所以有什么方法可以确保在调用 componentDidMount 之前完全加载状态

【问题讨论】:

  • 请提供更多代码示例。你是如何安装减速器的?什么是组件层次结构?

标签: reactjs redux react-redux redux-thunk


【解决方案1】:

您需要在componentWillReceiveProps 或新的getDerivedStateFromProps 中执行此操作,如果在安装后该值尚不可用。检测到 fk_branch_id 刚刚从 falsy 变成了truthy:

if (!this.props.fk_branch_id && nextProps.fk_branch_id) {
    // send request with nextProps.fk_branch_id
}

【讨论】:

    猜你喜欢
    • 2019-01-19
    • 2021-01-17
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    • 2018-01-07
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多