This is my component A:
const GenericTextInput = (props) => {
return (
<TextInput
style={styles.input}
{...props}
/>
);
};
This is my 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}
/>
</View>
);
};
I want to add a specific styling to the 2nd GenericTextInput
in my component B. In the component A I want to use the spread operator
(it is so convenient).
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. A, and the second style
prop
will completely overwrite the first one.
What is the proper way of adding a specific styling to the 2nd GenericTextInput
?
With prop-types or TypeScript, we can tell React the props we are passing for a given component, what their values should be, and whether they are optional. In plain React, be aware that passing props can be something you forget to do. Also you might not pass a certain prop to a component, if you so choose.
The children prop allows us to compose our components in powerful ways. This is especially helpful when we need to wrap one component around another, such as for styling, or to pass more component data to the children components to give two examples:
In plain React, be aware that passing props can be something you forget to do. Also you might not pass a certain prop to a component, if you so choose. 2. React props passed with just their name have a value of true Every prop must be given an associated value that is provided after the equals operator.
Using name as a prop lets us customize the Greeting component, so we can reuse that component for each of our greetings. This example also uses the Greeting component in JSX, similar to the Core Components.
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:
const {style, ...rest} = props;
return (
<TextInput
style={[styles.input, style]}
{...rest}
/>
)
Note that this requires the transform-object-rest-spread Babel plugin. 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With