[英]React Native: what is a proper way to pass style as props, when using spread operator on the child component
This is my component A: 这是我的组件A:
const GenericTextInput = (props) => {
return (
<TextInput
style={styles.input}
{...props}
/>
);
};
This is my component B: 这是我的组件B:
const SignIn = (props) => {
return (
<View>
<GenericTextInput
placeholder="Email"
autoFocus={true}
keyboardType="email-address"
returnKeyType="next"
autoCorrect={false}
placeholderTextColor='rgba(250,248,244, .4)'
underlineColorAndroid='rgba(250,248,244, .7)'
/>
<GenericTextInput
placeholder="Password"
placeholderTextColor='rgba(250,248,244, .4)'
underlineColorAndroid='rgba(250,248,244, .7)'
secureTextEntry={true}
/>
</View>
);
};
I want to add a specific styling to the 2nd GenericTextInput
in my component B . 我想为我的组件B中的第二个
GenericTextInput
添加一个特定的样式。 In the component A I want to use the spread operator
(it is so convenient). 在组件A中我想使用
spread operator
(它非常方便)。
If I simply make: 如果我只是做:
//component A
const GenericTextInput = (props) => {
return (
<TextInput
style={[styles.input, props.styles]}
{...props}
/>
);
};
//component B
const SignIn = (props) => {
return (
<View>
<GenericTextInput
placeholder="Email"
autoFocus={true}
keyboardType="email-address"
returnKeyType="next"
autoCorrect={false}
placeholderTextColor='rgba(250,248,244, .4)'
underlineColorAndroid='rgba(250,248,244, .7)'
/>
<GenericTextInput
placeholder="Password"
placeholderTextColor='rgba(250,248,244, .4)'
underlineColorAndroid='rgba(250,248,244, .7)'
secureTextEntry={true}
style={styles.customStyleForSecondInput}
/>
</View>
);
};
I'll have 2 style
props
in comp. 我将在comp中拥有2个
style
props
。 A , and the second style
prop
will completely overwrite the first one. A ,第二个
style
prop
将完全覆盖第一个。
What is the proper way of adding a specific styling to the 2nd GenericTextInput
? 将特定样式添加到第二个
GenericTextInput
的正确方法是什么?
I usually destructure the named property ( style
) out for the object, and use the rest operator to collect the remaining props into a new object: 我通常为对象解构出命名属性(
style
),并使用rest运算符将剩余的props收集到一个新对象中:
const {style, ...rest} = props;
return (
<TextInput
style={[styles.input, style]}
{...rest}
/>
)
Note that this requires the transform-object-rest-spread Babel plugin. 请注意,这需要transform-object-rest-spread Babel插件。 This plugin is included in the React Native Babel preset, so it will work out of the box -- but may not work in other JavaScript environments without this plugin.
此插件包含在React Native Babel预设中,因此它可以开箱即用 - 但如果没有此插件,可能无法在其他JavaScript环境中使用。
If you want your custom styles to completely overwrite the preset ones, I believe you can do something like 如果您希望自定义样式完全覆盖预设样式,我相信您可以做类似的事情
style={props.style || styles.input}
but I think you're asking how to have the custom styles overwrite some of the preset ones. 但我想你问的是如何让自定义样式覆盖一些预设样式。 In that case it would be
在那种情况下,它会
style={[styles.input, props.style]}
since you're passing the property down as 'style' not 'styles' you just drop the s at the end. 既然你把房产作为'风格'而不是'风格',你只需将它们放在最后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.