1
  1. 在antd中,我想通过一个数组循环出多个<TimePicker /> 组件,并在组件触发onChange时,将对应的值setState到对应下标的time.
...
this.state = {
  peroidTimeArr: [
    {
      day: 4,
      time: '11:57:22',
      hour: 2,
      key: 1
    },
    {
      day: 3,
      time: '11:57:22',
      hour: 2,
      key: 2
    },
  ]
}
...

timeChange(time, timeString)=>{
  console.log(time)
  console.log(timeString)
}


render() {
  return (
    {this.state.peroidTimeArr.map(function(item, index){
      return (<TimePicker data-index={item.key} value={moment(peroidTimeArr[i].time, 'HH:mm:ss')} onChange={ timeChange } />)
    })}
  )
}

antd TimePicker

文档里提到,onChange的参数是function(time: moment, timeString: string): void,只能在change事件中国获取到timetimeString,没办法获取到下标key

不知道网友们有没有什么奇淫巧技,可以获得这个值key?

1个回答

1
onChange={ ()=>timeChange(item.key) }

撰写答案