[英]How not to reexplicit instance components properties in generic components?
在反应中,我实现了这样的通用组件:
export function CustomTextInput(props) {
return (
<TextInput
placeholder={props.placeholder}
onChangeText={props.onChangeText}
style={{margin:20}}
/>
)
}
我像这样使用它们:
<CustomTextInput
placeholder="My placeholder"
onChangeText={secretCode => setSecretCode(secretCode)}
/>
有没有办法不必重新显式泛型组件中的每个属性? 例如,通过定义这样的通用组件:
export function CustomTextInput(props) {
return (
<TextInput
props={props}
style={{margin:20}}
/>
)
}
...同时仍然为组件实例保持相同的实现。
您可以使用spread syntax
传输道具,如下所示。
export function CustomTextInput(props) {
return (
<TextInput
{...props}
style={{margin:20}}
/>
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.