簡體   English   中英

如何不在泛型組件中重新定義實例組件屬性?

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM