我正在做一个React项目,在我的项目中我有两个按钮,对于第一个按钮,我为第二个按钮分配了一个状态,我写了一个函数,我也分配了一个状态。但是我的onClick函数不起作用。请帮我解决这个问题。
这是App.js
import React, { useState } from "react";
import { Button } from "antd"
import 'antd/dist/antd.css';
import "./App.css";
const App = () => {
const [buttonOne, setButtonOne] = useState("red")
const [buttonTwo, setButtonTwo] = useState("blue")
const buttonTwoBackgroundColor = () => {
setButtonTwo({
backgroundColor: "red",
border: "red"
})
}
return (
<div>
<Button style={{backgroundColor: buttonOne, border: buttonOne}} className="one" type="primary">First</Button>
<Button style={{backgroundColor: buttonTwo, border: buttonTwo}} onClick={buttonTwoBackgroundColor} className="two" type="primary">Second</Button>
</div>
)
}
export default App
这是App.css
.one {
margin-right: 5px;
margin-left: 250px;
margin-top: 50px;
}
.two, .three, .four, .five {
margin-right: 5px;
}
发布于 2021-07-16 19:06:58
在function buttonTwoBackgroundColor中,可以将setButtonTwo设置为对象。它应该是一个字符串。
const buttonTwoBackgroundColor = () => {
setButtonTwo("red")
}
https://stackoverflow.com/questions/68407888
复制相似问题