将一个元素数组渲染进 DOM

index.js

import React, { Component } from 'react'
import ReactDOM from 'react-dom'

class Bar extends Component {
  constructor() {
    super()
    this.state = {
      list: ['Lilei', 'Hanmeimei', 'Lihua'],
    }
  }
  clickHandler = value => {
    alert(value)
  }
  render() {
    const btns = this.state.list.map((item, index) => {
      return (
        <button key={index} onClick={this.clickHandler.bind(this, item)}>
          {item}
        </button>
      )
    })
    return <div>{btns}</div>
  }
}

ReactDOM.render(<Bar />, document.getElementById('root'))

posted on 2021-09-10 10:16  aisowe  阅读(106)  评论(0编辑  收藏  举报

导航