简体   繁体   English

React useState hook 使用初始状态值并忽略更新

[英]React useState hook uses the initial state value and ignores the update

I have written a component like below where I use React.useState to control the logic to show the button.我写了一个像下面这样的组件,我使用 React.useState 来控制显示按钮的逻辑。

If I click the button, it will reveal a sub-button, which was hidden initially, then, if I click on the sub button, it will reveal some content, but the second operation never gets executed.如果我点击按钮,它会显示一个最初隐藏的子按钮,然后,如果我点击子按钮,它会显示一些内容,但第二个操作永远不会被执行。

Below is the code:下面是代码:

MyButton code:我的按钮代码:

    export default observer(function MyButton() {
       ...
       const [subButtonHidden, setSubButtonHidden] = React.useState<boolean>(true)
       ...
       const onClickSubButton = () => {
           if (!subButtonHidden) {
              displaySubButtonContent(); // never ever reach here
           }
           setSubButtonHidden(!subButtonHidden);
       }

       return (
          <Subbutton
            ...
            subButtonHidden={subButtonHidden}
            onClickSubButton={onClickSubButton}
          />
       ); 
});

SubButton code:子按钮代码:

export default observer(function SubButton(props: ...) {
    ....
    const onClickSubButton = React.useCallback(() => {
        props.onClickSubButton();
    }, []);
    ....
    
    return props.subButtonHidden ? null : (<div>...</div>);
}

It turns out that subButtonHidden can be successfully updated as false for the very first click, hence show the sub button, but if I click the sub button, subButtonHidden will be somehow reset to true, if I click on the sub button again, subButtonHidden will still be true, even setSubButtonHidden(false) has been executed, no matter what it just doesn't take the updated values.事实证明,第一次单击时 subButtonHidden 可以成功更新为 false,因此显示子按钮,但是如果我单击子按钮,subButtonHidden 将以某种方式重置为 true,如果我再次单击子按钮,subButtonHidden 将仍然是真的,即使 setSubButtonHidden(false) 已经被执行,不管它只是不接受更新的值。 Why is it behaving like that?为什么会这样?

In the Subbutton , you have wrapped the onClickSubButton with useCallback with no dependencies.Subbutton ,您已经用onClickSubButton包装了useCallback并且没有依赖项。 That means its never changed.这意味着它从未改变。 and that means props.onClickSubButton is never changed inside the onClickSubButton .这意味着props.onClickSubButtononClickSubButton内永远不会改变。

So when the first time it is clicked, subButtonHidden is found as true and is set to false.所以当它第一次被点击时, subButtonHidden被发现为 true 并被设置为 false。 When you click the second time, since onClickSubButton is not updated because of useCallback, that means the same props.onClickSubButton was called in which subButtonHidden was true(because of closures).当您第二次单击时,由于onClickSubButton未因 useCallback 更新,这意味着调用了相同的props.onClickSubButton ,其中subButtonHidden为 true(因为闭包)。

So removing the useCallback around onClickSubButton should solve your issue.因此,删除useCallback周围的onClickSubButton应该可以解决您的问题。

I think you're just changing it directly that one time.我认为你只是直接改变它一次。 Instead you should setSubButtonHidden(prev => !prev);相反,您应该setSubButtonHidden(prev => !prev);

it should change depending on what it is (or was) not just directly changing it that one time from its original.它应该根据它是什么(或曾经是什么)而改变,而不仅仅是直接从原来的一次改变它。

How to toggle boolean state of react component? 如何切换反应组件的布尔状态?

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

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