假设这个。
我想在此处添加额外的回调。我想要什么吗?我找不到任何解决方案。
谢谢。
export default class NewClassXXX extends React.Component{
render()
{
newchildren = React.Children.map(this.props.children, (child) => {
//child is TouchableOpacity class in my code ***
//I want to add LongPress callback by coding HERE. (like below)
child.onLongPress = () => console.log('SUCESSS!!!'); //this code does not work.
return child;
}
return {newchildren}
}
使用 React.cloneElement
方法来实现此目的
export default class NewClassXXX extends React.Component{
render()
{
newchildren = React.Children.map(this.props.children, (child) => {
return React.cloneElement(child, {onLongPress: () => console.log('SUCESSS!!!')})
}
return {newchildren}
}