据我所知,如果父组件重新加载,那么它的所有子组件都将重新加载,除非它们实现shouldComponentUpdate().我发现这似乎不是真的.

我有三个部分:<DynamicParent/><StaticParent/><Child/>.<Parent/>个组件负责渲染<Child/>,但方式不同.

<StaticParent/>的render函数在运行前静态声明<Child/>,如下所示:

 <StaticParent>
    <Child />
 </StaticParent>

<DynamicParent/>在运行时处理<Child/>的动态接收和渲染,如下所示:

 <DynamicParent>
    { this.props.children }
 </DynamicParent>

<DynamicParent/><StaticParent/>都有onClick个侦听器,可以在单击时更改其状态并重新播放.我注意到当点击<StaticParent/>时,它和<Child/>都被重新命名.但当我点击<DynamicParent/>时,只有父项而不是<Child/>项被重新命名.

<Child/>是一个没有shouldComponentUpdate()的功能组件,所以我不明白为什么它不能重新启动.有人能解释为什么会这样吗?我在与这个用例相关的文档中找不到任何东西.

推荐答案

我将发布您的实际代码:

class Application extends React.Component {
  render() {
    return (
      <div>
        {/* 
          Clicking this component only logs 
          the parents render function 
        */}
        <DynamicParent>
          <Child />
        </DynamicParent>

        {/* 
          Clicking this component logs both the 
          parents and child render functions 
        */}
        <StaticParent />
      </div>
    );
  }
}

class DynamicParent extends React.Component {
  state = { x: false };
  render() {
    console.log("DynamicParent");
    return (
      <div onClick={() => this.setState({ x: !this.state.x })}>
        {this.props.children}
      </div>
    );
  }
}

class StaticParent extends React.Component {
  state = { x: false };
  render() {
    console.log("StaticParent");
    return (
      <div onClick={() => this.setState({ x: !this.state.x })}>
        <Child />
      </div>
    );
  }
}

function Child(props) {
  console.log("child");
  return <div>Child Text</div>;
}

在应用程序渲染中编写此代码时:

<StaticParent />

呈现的是:

 <div onClick={() => this.setState({ x: !this.state.x })}>
    <Child />
 </div>

实际上,发生的(大致)是这样的:

function StaticParent(props) {
  return React.createElement(
    "div",
    { onClick: () => this.setState({ x: !this.state.x }) },
    React.createElement(Child, null)
  );
}

React.createElement(StaticParent, null);

当你像这样渲染你的动态租金时:

<DynamicParent>
    <Child />
</DynamicParent>

这就是实际发生的情况(同样,粗略地说)

function DynamicParent(props) {
    return React.createElement(
        "div",
        { 
            onClick: () => this.setState({ x: !this.state.x }), 
            children: props.children 
        }
    );
}

React.createElement(
      DynamicParent,
      { children: React.createElement(Child, null) },
);

这就是两种情况下的子元素:

function Child(props) {
    return React.createElement("div", props, "Child Text");
}

这是什么意思?在StaticParent组件中,每次调用StaticParent的render方法时都会调用React.createElement(Child, null).在DynamicParent case 中,子对象被创建一次并作为props 传递.因为React.createElement是一个纯函数,所以它可能会被存储在某个地方以提高性能.

在DynamicParent case 中,让子元素的渲染再次运行的是子元素props 的变化.例如,如果父对象的状态用作子对象的props ,则在这两种情况下都会触发重新渲染.

我真的希望丹·阿布拉莫夫不要出现在 comments 中,把这个答案说成垃圾,写起来很痛苦(但很有趣)

Reactjs相关问答推荐

全局上下文挂钩-替代不起作用

Redux向后端发送请求时出现钩子问题

如何在Reac.js中通过子组件和props 调用父组件函数?

更改购物车中的产品数量后,PayPal Reaction/Next JS按钮未更新

如何访问子集合中的文档?

Reaction Google Maps API无法获取标题

无法找出状态正在被更改的位置

ReactJS:唯一项目的数量总和

如何使用 React 获取表单中的所有值?

对于 ref 上的可见 prop 或 open 方法来react 组件,哪种方式更好?

Next.js 和理智 | npm run build 时预渲染页面/时发生错误

ReactJS中使用setState方法时,Context内部的State未进行更新

使用登录保护 React 中的 SPA 应用程序 - 为什么这种方法不起作用?

在react 中从外部 api 渲染列表

使用 Jest/React 测试库时,如何测试由 setInterval 函数创建的 DOM 更改?

有没有办法根据 Rechart 中的 activeDot 更改值?

响应式媒体 React Tailwind

Cypress ,重试不再附加到 DOM 的元素

谁能根据经验提供有关 useEffect 挂钩的更多信息

如何在 React 中单击鼠标的位置动态添加 div?