如何让按钮在未单击时显示 +(加号),在单击时显示 -(减号)?
How do you make a button show a + (plus sign) when not clicked, and - (minus sign) when clicked?
请看下面的代码。
const [expanded, setExpanded] = useState(false);
const toggleExpanded = () => setExpanded(!expanded);
<button className="plus" onClick={toggleExpanded}> + </button>
你可以这样做:
https://codesandbox.io/embed/inspiring-dhawan-1z3dbx?fontsize=14&hidenavigation=1&theme=dark
function main () {
const [Sign, setSign] = useState('+')
function handleClick () {
if (Sign === '+') setSign('-')
else setSign('+')
}
return <button onClick={handleClick}>{Sign}</button>
}
请看下面的代码。
const [expanded, setExpanded] = useState(false);
const toggleExpanded = () => setExpanded(!expanded);
<button className="plus" onClick={toggleExpanded}> + </button>
你可以这样做: https://codesandbox.io/embed/inspiring-dhawan-1z3dbx?fontsize=14&hidenavigation=1&theme=dark
function main () {
const [Sign, setSign] = useState('+')
function handleClick () {
if (Sign === '+') setSign('-')
else setSign('+')
}
return <button onClick={handleClick}>{Sign}</button>
}