我正在使用第 3 方组件,其中 customComponent 道具接收一个函数,所以我有这个
<Form customComponent={() => <input type='text'/>}
但后来我有条件,所以我决定将我的 customComponent 放在函数级别
我以另一种方式做到了这一点
renderCustomComponent = () => {
//whatever logic goes here
return () => < input type = 'text' / >
}
render() {
return <Form customComponent = { this.renderCustomComponent } >
}
为什么我在 Form 中什么都没有?这两个不一样吗?第二种方法有问题。
我认为你应该这样做,因为如果 customComponent 有验证,即它需要函数,那么@Giang Le解决方案将不起作用
renderCustomComponent = () => {
//whatever logic goes here
return <input type = 'text' />
}
render() {
return <Form customComponent = { this.renderCustomComponent } >
}
或者
renderCustomComponent = (props) => {
//whatever logic goes here
return <input type = 'text' />
}
render() {
return <Form customComponent = { (props)=> this.renderCustomComponent(props) } >
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句