reactjs / reactjs/react.dev

Lifecycle with updates at multiple hierarchy levels

未关闭
#1,210 7 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
JavaScript
星标
11.8k
派生
7.9k
平均合并
1 天 11 小时
30 天内合并 PR
11

描述

This seems to be a particular lifecycle that wasn't considered when making the componentWillReceiveProps function deprecated. Or, perhaps I just cannot find a built-in way in React on how to do this properly. In either case, some guidance would be highly appreciated.

In my project, a top-level component fetches data from a service and then passes it, when received, to lower-level components as props (i.e., the higher-level component is fully controlled). These lower-level components are fields that display the retrieved data, and allow adding and removing data items afterwards. Clearly, these operations need to be reflected in the state (and also sent to the restful service, but this is inconsequential here). Hence, it would make sense to make the state the "single source of truth" since the data could be updated by the lower-level component ("field" from now on) at any point, requiring a re-render each time (?)

When first creating the field (i.e., in the constructor), this data is not yet available, since it's fetched from the service. This means that a method such as componentWillReceiveProps or getDerivedStateFromProps should be used to derive the initial state from the props (?) Since componentWillReceiveProps is deprecated I recently tried replacing it with getDerivedStateFromProps. However, this function is called "on every render, regardless of the cause" (e.g., because of a state change), instead of componentWillReceiveProps "which only fires when the parent causes a re-render" (see here). Hence, any local state change (e.g., removing a data item) will also cause the getDerivedStateFromProps function to be called (including ones caused by getDerivedStateFromProps ...).

Currently, to differentiate between state & props updates, I introduced the following function, which only derives the state from the props once, and, after any local state change, will ignore changes in props. The function is called from the field's getDerivedStateFromProps function (ignoreFunction depends on the particular field, and returns whether state and props include the same data). Note that a local state change involves setting an update field to true in the state.

static getGroundStateFromProps(props, state, ignoreFn) {
    // nothing in state or props was updated
    if (ignoreFn(state, props))
      return null

`    // some relevant update occurred; either state or props
    else {
      // in case state has been updated once, consider it "ground truth"
      if (state.updated)
        return null
      else
        return { values: props.values }
    }
  }

Would this be a suitable pattern to deal with this issue?

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

查看 issue 中链接的 React 组件生命周期文档,尤其是 componentWillReceiveProps 和 getDerivedStateFromProps,并结合所描述的受控 parent 与可在本地编辑的 child 之间的流程。明确在保留 child 更新的同时接收异步 props 的受支持模式,并记录区分由 props 驱动的状态变更和本地状态变更的指导。完成标准是:该场景具有明确推荐的方法,且不依赖已弃用的生命周期行为。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, react
领域
documentation, frontend
Issue 类型
文档
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
30/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。