简体   繁体   English

反应组件组成但令人困惑

[英]react component composition but confusing

so I have a code snippet here basically presenting the idea Dan mentioned which is to lift contents up in react to naturally improve performance and write cleaner code.所以我在这里有一个代码片段基本上展示了 Dan 提到的想法,即提升内容以自然地提高性能并编写更清晰的代码。 In the InputField component, the Button always renders, this is not the behavior I want, it should skip rendering the Button component .so what it should do is skip the render because Button is inputfield's children props, and if children props does not change, react will skip the render.在 InputField 组件中,Button 总是渲染,这不是我想要的行为,它应该跳过渲染 Button 组件。所以它应该做的是跳过渲染,因为 Button 是 inputfield 的子道具,如果子道具没有改变, react 将跳过渲染。

similar concept example explained by Dan: "When the color changes, ColorPicker re-renders. But it still has the same children prop it got from the App last time, so React doesn't visit that subtree" Dan 解释了类似的概念示例:“当颜色改变时,ColorPicker 重新渲染。但它仍然具有上次从应用程序获得的相同子级道具,因此 React 不会访问该子树”

so I am confused here, is that the problem with multiple children props, because InputField has value, onChange, children props, and one of them change, react decides to update others?所以我在这里很困惑,是不是多个子道具的问题,因为InputField有值,onChange,子道具,其中一个改变,反应决定更新其他? I have an example here that kinda proves it's not the problem of multiple props.这里有一个例子可以证明这不是多个道具的问题。

Edit: follow up question, in the real world, top level components must have all kinds of useState, state update etc. would that mean the "lift contents up" (component composition) is not practical in the real world?编辑:后续问题,在现实世界中,顶级组件必须具有各种 useState、状态更新等。这是否意味着“提升内容”(组件组合)在现实世界中不实用? just from the perspective of saving some rendering computing power.只是从节省一些渲染计算能力的角度来看。 (we know it has other benefits like help with some prop drilling) Are there any real world implementations, code examples?? (我们知道它还有其他好处,例如帮助进行一些道具钻探)是否有任何真实世界的实现、代码示例?

Thanks @Jacob Smit in the comments.感谢@Jacob Smit 在评论中。 solved my problem.解决了我的问题。 My Button component is still two low in the component structure, the 'lift contents up' way is to lift the content(component) up in your code so that when you setState() in the lower component, it won't affect that content(component).so the lower component keeps the props that are bound to pass down.我的 Button 组件在组件结构中仍然是两个低层,“提升内容”的方式是将代码中的内容(组件)提升,这样当您在较低的组件中 setState() 时,它不会影响该内容(component).so 下层组件保留了必定要向下传递的 props。 For here, exactly as Jacob said my setState() is triggering re-render of Button and InputField, so Button will be rendered.对于这里,正如 Jacob 所说,我的 setState() 正在触发 Button 和 InputField 的重新呈现,因此将呈现 Button。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM