简体   繁体   English

useState 中的异步

[英]Asynchron in useState

My code is:我的代码是:

<blink>
  const [thisButtomSelected, setThisButtomSelected] = useState({
    thisVal: 1111,
    thisIndex: 1111,
  })
   const onClick = e => {  
    setThisButtomSelected({ thisVal: e.currentTarget.value, thisIndex: e.currentTarget.id });
  }

  <li id="list" key={item.id}>
    <button
      value={item.value}
      id={index}
      className={isEqual(thisButtomSelected, { thisVal: item.value, thisIndex: index }) 
        ? 'button-selected' : 'button'
      }
      onClick={onClick}
    >
      {item.displayValue}
    </button>
</li>
</blink>  

Whenever I click the button in the app it makes thisButtomSelected always equal to the previous value.每当我单击应用程序中的按钮时,它都会使thisButtomSelected始终等于先前的值。

In this case, is it not possible to change the button's style to what is selected?在这种情况下,是否无法将按钮的样式更改为选定的样式?

I can't understand where the problem is?我不明白问题出在哪里?

the isEqual function you are using is not the native JavaScript function to use, instead, you can import Loadash and use it to compare two objects.您正在使用的 isEqual function 不是本机 JavaScript function 使用,相反,您可以导入 Loadash 并使用它来比较两个对象。 to use the instruction would be like:使用指令就像:

_.isEqual(object, other);

As I have mentioned before you have to install the loadash package and import it into your JS file.正如我之前提到的,您必须安装 loadash package 并将其导入到您的 JS 文件中。

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

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