【问题标题】:Will the line right after the sync action notice the changes immediately?同步操作之后的线路会立即注意到更改吗?
【发布时间】:2018-12-15 04:46:32
【问题描述】:

如果在块中调度同步操作,它下面的行会注意到变化吗?

// In a compoent connected to the reducer
class someComponent extends Component {

  const interuptOrNot = () => {
    this.props.dispatchChangeSomeStateAction({ payload: 2 })
  }

  render() {
    this.interuptOrNot()
    console.log(this.props.someState)
    return(<SomeComponent>)
  }

}

如果 reducer 的原始 props 值等于 1,那么记录器的输出信息是什么。 1 还是 2?或者第一次渲染调用没有输出,但它会输出 2 用于最新状态的重新渲染。

我知道在渲染函数中发送动作是个坏主意,但是如果我调度一些动作来改变组件使用的减速器的状态会发生什么?

如果dispatchChangeSomeStateAction 向reducer 分派一个同步redux action,我在render 函数中调用这个函数怎么办?当前渲染是否会因为状态过期而中止?

【问题讨论】:

    标签: reactjs react-redux


    【解决方案1】:
        console.log(this.props.someState)
    

    将返回 1。

    当前的渲染函数将使用当前的 prop 值运行。

    当组件状态改变时it will re-render.

    使用componentWillReceiveProps 跟踪新的/更新的道具。

    Props are immutable,如果它们更新了它们,您将跟踪 componentWillReceiveProps 中的更改并将更新传递给该组件的状态。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-10
      • 2022-09-28
      相关资源
      最近更新 更多