reactjs - jsx - 中的循环 map, index
访问量: 25
refer to: https://legacy.reactjs.org/docs/lists-and-keys.html
循环中,如果对象是一个空数组([]) , 那么map中的内容不会运行。例如:
export default class CalculationViewResult extends Component {
state = {
results: [],
}
render() ...
{
this.state.results.map((item, index) => (
// [{"value": 100}, {"value": 200}].map((item, index) => (
<p key={index}>{item.bank_count}</p>
))
}
在上面代码中:
1. 如果this.state.results = [] 时, <p>中的内容不会显示,所以item.bank_count 不会报错。
2. 每个iterator (也就是<p>) 中必须有个key的属性。否则reactjs会报错。