简体   繁体   English

如何在 React 中更改状态

[英]How to change state in React

I am having a simple state, which defines a price of the coffee for employees, if the radio button is checked.如果选中单选按钮,我有一个简单的状态,它定义了员工咖啡的价格。

const [coffee, setCoffee] = useState(0);    
const [checkedCoffee, setCheckedCoffee] = useState(true);

This is how I am setting up the new state:这就是我设置新状态的方式:

 useEffect(() => {
        if (checkedCoffee) {
            setCoffee(employees * 40);
        } else {
            setCoffee(0);
        }
}, [coffee])

But I want to have another option, which will reduce the coffee price 50% and this is how I am trying to handle it:但我想有另一种选择,它将咖啡价格降低 50%,这就是我试图处理它的方式:

 const handleDivision = () => {
        setCoffee(coffee / 2);
    };

And then just calling handleDivision in onClick button.然后在onClick按钮中调用handleDivision

 <button onClick={handleDivision}>division button</button>

The result is just refreshing the price divided by 2 - so something is happening, but it never actually set up the 50% less price.结果只是刷新了除以 2 的价格 - 所以发生了一些事情,但它实际上从未设置过 50% 的价格。

Where does my code conflicts?我的代码在哪里冲突?

You can see in the documentation for useEffect that it states:您可以在useEffect的文档中看到它指出:

The function passed to useEffect will run after the render is committed to the screen.传递给 useEffect 的函数将在渲染提交到屏幕后运行。 Think of effects as an escape hatch from React's purely functional world into the imperative world.将效果视为从 React 的纯函数式世界进入命令式世界的逃生口。

Based on the statement from the documentation, it looks like your useEffect function will execute again after you've clicked your button and refresh the value based on the useEffect function eg根据文档中的声明,在您单击按钮并根据useEffect函数刷新值后,您的useEffect函数似乎将再次执行,例如

 if (checkedCoffee) {
     setCoffee(employees * 40);
 } else {
     setCoffee(0);
 }

In order to fix this you can remove the useEffect call in component and execute the above code inplace or as a seperate function call.为了解决这个问题,您可以删除组件中的useEffect调用并就地或作为单独的函数调用执行上述代码。

Try this :试试这个 :

<button 
     onClick={()=>{
          handleDivision()
     }}>
              division button
</button>`

Instead of :代替 :

<button onClick={handleDivision}>division button</button>

Check out this sandbox:看看这个沙箱:

https://codesandbox.io/s/optimistic-cache-4c9ud?file=/src/App.js https://codesandbox.io/s/optimistic-cache-4c9ud?file=/src/App.js

Try this:试试这个:

 const handleDivision = () => {
    setCoffee(prevCoffePrice => prevCoffePrice / 2);
};

Add checkedCoffee to useEffect dependency list.checkedCoffee添加到useEffect依赖列表中。

useEffect(() => {
  if (checkedCoffee) {
    setCoffee(employees * 40);
  } else {
    setCoffee(0);
  }
}, [checkedCoffee]);

And then:然后:

...

const handleDivision = () => {
  setCoffee(coffee / 2);
};

return <button onClick={handleDivision}>division button</button>;
...

Look at the example here → https://codesandbox.io/s/brave-grothendieck-floly?file=/src/App.js看这里的例子→ https://codesandbox.io/s/brave-grothendieck-floly?file=/src/App.js

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

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