【发布时间】:2017-12-13 00:49:38
【问题描述】:
我正在尝试添加到我现有的名为 player 的数组(这是一个外部文件“players.js”,已导入我的父级)。
我会写:
players.push({
name: 'Maul',
id: 26
});
这可以作为 ID 用于键并且名称在数组中更新。
但是如果我写
handleAddPlayer = () => {
console.log('i am working');
players.push({
name: 'Maul',
id: 26
});
};
我的按钮上有
<div>
<h4>Add A Player</h4>
<input />
<button onClick={this.handleAddPlayer}>Press to Add Player</button>
</div>
而我的状态如下:
this.state = {
id: players.id,
totalScore: 0,
countInfo: [],
evilName: '',
color: '#6E68C5',
scoreColor: '#74D8FF',
fontAwe: 'score-icon',
incrementcolor: '',
scoreNameColor: 'white',
glow: '',
buttonStyle: 'count-button-start',
players,
};
它不起作用。
从这里我有两个问题:
为什么我点击按钮不更新,但之前不能作为功能使用?
我想在输入中输入一个名称并生成一个唯一键,并将其添加到我的数组 player.js 中
我尝试过连接,但没有取得很大成功。
【问题讨论】:
-
您能告诉我们您遇到的错误吗?不是“无法读取未定义的属性‘push’”吗?
-
我没有收到任何错误,因为没有任何反应
标签: javascript arrays reactjs ecmascript-6