我没有得到如何在渲染函数外部访问map()方法中定义的变量,以便在单击按钮后警告变量值,即使我在数组中传递键,它仍然在抛出我Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method.
export default class List extends React.Component {
constructor(props) {
super(props);
this.handleEditClick = this.handleEditClick.bind(this);
this.list = ''
}
handleEditClick() {
alert(this.list);
}
render() {
return(
<div>
{
this.props.listArr.map((list,index) => {
return(
<div>
<li key={index}>{list}
<div style={{float: 'right'}}>
<button className="btn btn-danger btn-xs glyphicon glyphicon-trash"/>
</div>
<div style={{float: 'right'}}>
<button className="btn btn-primary btn-xs glyphicon glyphicon-pencil" onClick={this.handleEditClick} />
</div>
<hr/>
</li>
</div>
)
})
}
</div>
);
}
}