【发布时间】:2025-12-27 05:10:12
【问题描述】:
我有一个文本框,一个可点击按钮和另一个不可点击按钮,用于在按下可点击按钮时显示一个数字我希望文本框中的值显示在另一个按钮中。 this.state 正在更新但未显示。
这是我第一次使用 react,请给我任何反馈。
class GameBoard extends React.Component {
render() {
return (
<div className="gameBoard">
<table>
<tbody>
<tr>
<th><input id="trips" className="inp"></input></th>
<th><button onClick={() => this.props.onClick("trips")}>place bet</button></th>
<th><button className="bettingSquere" >{this.props.game.trips}</button></th>
</tr>
</tbody>
</table>
</div>
);
}}
class App extends Component {
constructor(props) {
super(props);
this.state = {
trips: 0,
};
}
handleClick(type) {
var state = this.state;
state.trips=document.getElementById("trips").value;
this.state=state;
}
render() {
return (
<div align="center">
<GameBoard game={this.state} onClick={i => this.handleClick(i)} />
</div>
);
}
}
export default App;
【问题讨论】:
-
更新状态时需要使用
this.setState方法。不能通过直接修改来改变状态。
标签: javascript reactjs