简体   繁体   English

反应组件背后显示 state 变化的原因是什么?

[英]What is the reason behind react component showing the changes of state inside them?

When we have many components in react project and sometimes we use multiple pre-made components for making a page.当我们在 react 项目中有很多组件时,有时我们会使用多个预制组件来制作页面。 While using onChange inside a component and showing the result of the state , in this case, what functionality of components allows the value render of state and how it works when we have multiple components inside other components.在组件内部使用onChange并显示state的结果时,在这种情况下,组件的哪些功能允许state的值呈现以及当我们在其他组件中有多个组件时它是如何工作的。 Here is an Ex...这是一个前...

 function Component() {
  const [value, setValue] = React.useState()
  const handleChange = val => {
    setValue(val)
  }
 return (
    <React.Fragment>
      <Compo1  //perform adding +1 
        onChange={handleChange}
      />
      Value: {value} // 1
     {console.log("value", value)} // showing right value
      <Compo2>
        <Compo3>
          <Compo1  //perform adding +1
            onChange={handleChange}
          />
          Value:{value} // undefined
          {console.log("value", value)} // showing right value
        </Compo3>
        {console.log("value", value)} // showing right value
      </Compo2>
    </React.Fragment>
  )
}
render(<Component />)

In this case why console is showing the right value but the state variable value is showing undefined.在这种情况下,为什么console显示正确的值但state变量value显示未定义。

The only way I can get that code to do what you say it does is when you incorrectly use React.memo on Compo3:我可以让该代码执行您所说的唯一方法是当您在 Compo3 上错误地使用React.memo时:

 const Compo1 = ({ onChange }) => ( <button onClick={() => onChange(Date.now())}>+</button> ); const Compo2 = ({ children }) => <div>{children}</div>; const Compo3 = React.memo( function Compo3({ children }) { return <div>{children}</div>; }, () => true//never re render unless you re mount ); function Component() { const [value, setValue] = React.useState(88); const handleChange = React.useCallback(() => { setValue((val) => val + 1); }, []); return ( <React.Fragment> <Compo1 //perform adding +1 onChange={handleChange} /> works: {value}----- <Compo2> <Compo3> <Compo1 //perform adding +1 onChange={handleChange} /> broken:{value}----- </Compo3> </Compo2> </React.Fragment> ); } ReactDOM.render( <Component />, document.getElementById('root') );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script> <div id="root"></div>

Maybe you can do the same if you do some wacky stuff with shouldComponentUpdate如果你用shouldComponentUpdate做一些古怪的事情,也许你也可以这样做

A component will render when:组件将在以下情况下呈现:

  1. When the parent renders the child and the child is a functional component (not wrapped in React.memo)当父级渲染子级并且子级是功能组件时(未包装在 React.memo 中)
  2. When the parent renders the child with different prop values than the previous render.当父级使用与前一个渲染不同的道具值渲染子级时。
  3. When value in [value,setValue]=useState() or when this.state changes (when state changes).[value,setValue]=useState()中的值或this.state改变时(当 state 改变时)。
  4. When someContext in value = useContext(someContext) changes (even if value doesn't change).value = useContext(someContext)中的 someContext 发生变化时(即使 value 没有变化)。
  5. In most cases when value in value = useCustomHoom() changes but this is not guaranteed for every hook.在大多数情况下,当 value value = useCustomHoom()中的值发生变化时,但这并不能保证每个钩子。
  6. When Parent renders and passes a different key prop to Child than the previous render (see 2).当 Parent 渲染并向 Child 传递与前一个渲染不同的 key prop 时(参见 2)。 This causes the Child to unmount and re mount as well.这也会导致 Child 卸载并重新安装。

In the example the Compo3 wants to re render because Parent is re rendered due to a state change and passes different props (props.children).在示例中,Compo3 想要重新渲染,因为父级由于 state 更改而重新渲染并传递了不同的道具(props.children)。

Compo3 is not a functional component because it's wrapped in React.memo. Compo3 不是函数式组件,因为它包装在 React.memo 中。 This means that Compo3 will only re render if props changed (pure component).这意味着 Compo3 只有在 props 改变时才会重新渲染(纯组件)。

The function passed as the second argument to React.memo can be used to custom compare previous props to current props, if that function returns true then that tells React the props changed and if it returns false then that tells React the props didn't change.作为第二个参数传递给 React.memo 的 function 可用于自定义比较以前的道具与当前道具,如果 function 返回 true 则告诉 React 道具已更改,如果返回 false 则告诉 React 道具未更改.

The function always returns true so React is never told that the props changed. function 总是返回 true ,所以 React 永远不会被告知 props 改变了。

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

相关问题 无故改变反应状态 - React state magically changes for no reason React Hooks 状态更新落后一步。 出于这个原因,我的更改无法正常工作 - React Hooks State update one step behind. For this reason my changes are not working correctly 通过侦听反应挂钩内的套接字事件或在中间件内处理它们来更新组件 state? 每个都有什么缺点? - Updating component state by Listening to socket events inside react hooks or handling them inside middleware? what are the downsides of each one? 通过在组件内部执行函数来响应 redux 状态的变化 - React to redux state changes by executing function inside of component 我应该使用什么生命周期方法来监听 state 的变化并在反应 class 组件中相应地更新其他 state - What lifecycle method should i use to listen for changes in state and update other state accordingly in react class component React Native with Redux:状态更改未显示在控制台中 - React Native with Redux : state changes not showing in console 当状态改变时,React 视频组件不会更新 - React Video Component not updating when state changes 在 state 更改后,React 组件不呈现 - React Component does not render after the state changes 当 redux state 更改时更新反应组件 - Updating react component when redux state changes React Childs 组件更改他的父母 state - React Childs component changes his parents state
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM