简体   繁体   English

React Native:当在子组件上使用spread运算符时,将样式作为props传递的正确方法是什么

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

相关问题 使用扩展运算符更改React组件的样式? - Using the spread operator to change the style of a React component? 将道具传递给子组件 - React Native - Pass props to child component - React Native 使用 typescript 为 props 的扩展运算符反应功能组件 - React functional component with spread operator for props using typescript 问题使用名为 function 的组件传递道具并使用 React 传播运算符 - Problem pass props to component using called function and spread operators with React 将prop传递到React中的组件的正确方法是什么? - What is the correct way to pass props to component in React? 如何使用 React Native 中的 props 将数组从父组件传递到子组件? - How to pass an array from a parent component to child component using props in React Native? 扩展运算符将所有其他道具传递给组件。 反应.js - Spread operator to pass all other props to a component. React.js 将道具从子级传递到主要组件(React Native) - Pass props to the main component from child (React Native) 处理使用 object 破坏和 object rest 的组件的道具类型的正确方法是什么...传播收集道具 - What is the proper way to handle prop-types for a component that uses object destructing and object rest...spread to collect props 如何使用 props 和 spread 运算符从子组件操作 state? - How do I manipulate state from a child component using props and spread operator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM