我找到了解决方案。先说我在我的错误解决方案中使用了在构造函数中创建ref的方法
class DummyClass extends Component {
constructor(){
this.timePickerRef = React.createRef();
}
}
render() {
let timers = array.map( index => {
<TimePicker
ref={timepicker => timePickerRef = timepicker}
value={00}
onChange={(data) =>
{this.handleTimePcikerValueChange(data, index);}}
/>
}
return (
timers
)
}
}
我所做的是以下
忽略并删除 this.timePickerRef = React.createRef() 因为它不再是必要的
而 handleTimePcikerValueChange & render 中的代码如下:
handleTimePcikerValueChange = (value, index) => {
// do whatever manipulation you need
// and access the ref using the following
this[`timePicker_${index}`]
}
render() {
let timers = array.map( index => {
<TimePicker
ref={timepicker => this[`timePicker_${index}`] = timepicker}
value={00}
onChange={(data) =>
{this.handleTimePcikerValueChange(data, index);}}
/>
}
return (
timers
)
}
我没有发布处理添加时间选择器的代码,因为它无关紧要。我要感谢那些回复的人!