[英]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.