简体   繁体   English

为什么更新 state 时组件不重新渲染?

[英]Why doesn't component rerender when state is updated?

MultiScreens is passed an index prop that controls what it displays. MultiScreens 传递了一个 index 属性来控制它显示的内容。 Clicking Button triggers an update handler that changes the state prop that MultiScreens uses for its index.单击 Button 会触发一个更新处理程序,该处理程序会更改 MultiScreens 用于其索引的 state 属性。 When Button is clicked state gets changed but the indexState getting passed in doesn't seem to change.单击按钮时,state 会更改,但传入的 indexState 似乎没有更改。 How can I make MultiScreens rerender when indexState is changed?更改 indexState 时如何使 MultiScreens 重新呈现?

export default function Modal() {
    const [indexState, setIndexState] = useState({idx: 0});
    
    const onIncrement = (e) => {
        let newIndexState = {
            idx: indexState.idx + 1
        }
        setIndexState(newIndexState)
    }

  return (
        <>
            <MultiScreens index={indexState}/>
            
            <Button onClick={onIncrement}>
                increment index
            </Button>
        </>
  );
}

I was overriding the index inside MultiScreen.我正在覆盖 MultiScreen 中的索引。

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

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