如何在render函数外部访问map方法中定义的变量

时间:2017-11-05 23:58:46

标签: reactjs

我没有得到如何在渲染函数外部访问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>
  );
 }
}

0 个答案:

没有答案