Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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?

like image 761
Edgar Avatar asked Nov 17 '16 21:11

Edgar


People also ask

What is the use of prop-types in react?

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.

What is the use of children prop in react?

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:

How to pass a prop to a React component?

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.

Why do we use name as a prop in JSX?

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.


1 Answers

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.

like image 196
jevakallio Avatar answered Oct 19 '22 07:10

jevakallio