如何传递特定于索引的道具



这是平面列表中每个项的"+1"的代码。单击项目时,每个项目都有自己的计数。如何在计数器类之外访问此计数?我不知道如何传递它,因为它是特定于索引的?

constructor(props) {
super(props);
this.state = {
bgColor: [
'#7ED3EB',

'#FF9191',
'#B25E9D',
'#093A52',
'#FFA87C',
'#781330',
'#BAB3F2',
'#C2A0E0',
'#BF3D35',
'#8ADFF2',
'#49C144',
'#135088',
'#8053AE',
'#94BEA1',
'#93D9B4',
'#F66955',
'#F08346'

],
selectedColor: '',
count: 0,
}}
addCount = () => {
let newCount = this.state.count + 1;
console.log(this.state.count)
console.log(this.props.list.title)
this.setState({
count: newCount
}
);

};

下面这个renderitem就是被推到平面主义者的东西。

renderItem = ({item}) => {
const list = this.props.list
return (

<CounterButton list={item} newcount={this.state.count} updateList={this.props.updateList}  />

)

}

在全局作用域中创建newCount变量或在this.state = {}内部添加newCount : 0。然后很容易实现你想要的。

最新更新