import React from "react"
export default function App() {
let [isGoingOut, setisGoingOut] = React.useState("Yes")
function changeState(){
setisGoingOut(prevValue => {
return prevValue = "Yes" ? "No" : "Yes"
})
}
return (
<div className="state">
<h1 className="state--title">Do I feel like going out tonight?</h1>
<div className="state--value" onClick={changeState}>
<h1>{isGoingOut}</h1>
</div>
</div>
)
}
当我单击值“是”时,它会更改为“否”。 但是当我单击值“否”时,它不会更改为“是”。 这里有什么错误吗?
您在 setState 回调中使用赋值运算符
=
而不是相等比较运算符 ==
。
正确的是:
setisGoingOut(prevValue => {
return prevValue == "Yes" ? "No" : "Yes"
})