【发布时间】:2017-12-13 01:26:42
【问题描述】:
我有一个数组,我想从用户的输入中添加一个新名称。在我的组件中,我有:
<div>
<h4>Add A Player</h4>
<input
type="text"
placeholder="Enter a new name"
value={this.state.newPlayerName}
/>
<button onClick={this.handleAddPlayer}>
Press to Add Player
</button>
</div>
除名称外,与此功能一起使用:
handleAddPlayer = e => {
const players = this.state.players.slice(0);
players.push({
name: '',
id: getRandomInt(20, 55)
});
this.setState({
players: players,
newPlayerName: e.target.value
});
};
我试图在用户输入名称并提交时得到,它会在更新数组的函数中更新(如果这有意义,道歉,因为这对我来说仍然很新)。
在我的状态下,我有:
this.state = {
id: players.id,
totalScore: 0,
countInfo: [],
evilName: '',
color: '#6E68C5',
scoreColor: '#74D8FF',
fontAwe: 'score-icon',
incrementcolor: '',
scoreNameColor: 'white',
glow: '',
buttonStyle: 'count-button-start',
newPlayerName: '',
players
};
我不确定如何获取输入以将名称(或字符串)传递给数组,有人可以帮忙吗?
【问题讨论】:
标签: javascript arrays reactjs ecmascript-6