讓我先序說明我正在學習React,並且我仍然非常青。React.js遞增和遞減計數器映射不正確
我要去給代碼的必要部分:
我已經建立了由國家的方式利用增量和減量按鈕的櫃檯,他們工作得很好,直到我介紹和數組和地圖超過它。然後事情開始崩潰。我知道我的代碼是錯誤的,我知道有什麼不對,但我完全不知道甚至找什麼。
在我counting.js
我:
const players = [
{
name: "Jon Smith",
score: 10,
id: 1,
},
{
name: "Jon Doe",
score: 40,
id: 2,
},
{
name: "John Ham",
score: 30,
id: 3,
},
];
我已經映射在這裏:
class Counting extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0,
nameof: 'Full Name',
}
this.incrementCount = this.incrementCount.bind(this)
this.decrementCount = this.decrementCount.bind(this)
}
incrementCount(e) {
this.setState({
count: (this.state.count + 1),
})
}
decrementCount(e) {
this.setState({
count: (this.state.count - 1),
})
}
render() {
const listPlayers = players.map((players) =>
<Counter
key={players.id}
incrementCount={this.incrementCount}
decrementCount={this.decrementCount}
nameof={players.name}
count={players.score}
/>
);
return (
<div className="wrap">
<Titles header="Counter" subhead="A setState() project" subtitle="this will change" />
<h3>This doesn't work correctly</h3>
<ul>{listPlayers}</ul>
<ScoreList>
<h3> works</h3>
<li>
<Counter
incrementCount={this.incrementCount}
decrementCount={this.decrementCount}
nameof={this.state.nameof}
count={this.state.count}
/>
</li>
<li>
<Counter
incrementCount={this.incrementCount}
decrementCount={this.decrementCount}
nameof={this.state.nameof}
count={this.state.count}
/>
</li>
</ScoreList>
</div>
)
}
}
我已經導入我的Counter.js
這是由:
class Counter extends Component {
render() {
const { count } = this.props
const { decrementCount } = this.props
const { incrementCount } = this.props
const { nameof } = this.props
return (
<div>
<CountCell>
<Row style={{alignItems: 'center'}}>
<Col>
<CountButton
onClick={incrementCount}>
<Icon
name="icon" className="fa fa-plus score-icon"
/>
</CountButton>
</Col>
<Col >
<ScoreName>{nameof}</ScoreName>
</Col>
<Col >
<Score>{count}</Score>
</Col>
<Col>
<CountButton
onClick={decrementCount}>
<Icon
name="icon" className="fa fa-minus score-icon"
/>
</CountButton>
</Col>
</Row>
</CountCell>
</div>
)
}
}
所以遞增和遞減按鈕僅在全局工作,並且僅適用於我的靜態<li>
,而不是從陣列生成的。如果我有任何意義,我如何分別將我的inc/dec按鈕映射到每個<li>
而不是全局?
謝謝!
你看到一個錯誤的相應用戶的每個存儲數據?你看到的具體行爲是什麼? – christopher
當我點擊'常量listPlayers = players.map((玩家增加和減少按鈕)=> <計數器 關鍵= {} players.id incrementCount = {this.incrementCount} decrementCount = {this.decrementCount} nameof = {} players.name計數 = {} players.score /> );' 什麼也沒有發生,該名被映射在就好了,但我不能+或 - 比分 – sthig
我不是確定是否有意義 – sthig