简体   繁体   English

如何通过对象传递道具

[英]How to pass props via an object

I want to spread all the props passed *probably as an object to a child component.我想将所有传递的道具 *可能作为对象传播给子组件。

Consider I have a array of objects which is used to determine the type of the object and based that I have a map object which renders that object.考虑我有一个对象数组,用于确定对象的类型,并基于我有一个渲染该对象的地图对象。

So this is my array of objects所以这是我的对象数组

const inputFields = [
  {
    key: 'images',
    type:'otp', 
    label: "Upload all your images", 
    required: true, 
    helper: 'You can change your profile pic anytime from settings', 
    templateOptions:{
      noOfTextInput:5
    }
  }, 
  {
    key: 'name', 
    type: 'text', 
    label: `Your Full Name`,
    helper: 'Using your real name would make it more likely for you to get a match',
    required: true,
    templateOptions: { 
      placeHolder: 'Frank Murphy',
      templateStyle: styles.textStyle // refer to the style component
    }
  }]

Here type image means, I want the image component to render and type text means I want type Textinput to render这里输入image意味着,我希望image组件渲染,输入text意味着我希望输入Textinput渲染

Now Textinput can take many props which I want to spread it on my TextInput component (by many props, I mean all the props it supports).现在Textinput可以接受许多道具,我想将它传播到我的 TextInput 组件上(许多道具,我的意思是它支持的所有道具)。

  <TextInput  
      style={[{color: defaultColor, borderColor: defaultColor}, styles.defaultTextInputStyle, textInputStyle]}
      onChangeText={text => onChangeHandler(text)}
      keyboardType
      value={value} />

So how can I dynamically spread all the props user have passed and should user pass it in the array object?那么如何动态传播用户传递的所有道具,用户是否应该将其传递到数组对象中?

So this is how I achieved it.所以这就是我实现它的方式。

{
    key: 'name', 
    type: 'text', 
    label: `Your Full Name`,
    helper: 'Using your real name would make it more likely for you to get a match',
    required: true,
    templateOptions: { 
      componentProps: {
        placeholder: 'Frank Murphy'
      },
      templateStyle: styles.textStyle // refer to the style component
    }
  }

Added something as componentProps above and then I am destructuring that property and passing it like this在上面添加了一些作为componentProps东西,然后我正在解构该属性并像这样传递它

 <TextInput  
  {...componentProps}
  style={[{color: defaultColor, borderColor: defaultColor}, styles.defaultTextInputStyle, textInputStyle]}
  onChangeText={text => onChangeHandler(text)}
  value={value}
  /> 

Better pick and spread your props explicitely, if TextInput component does not support all of them:如果TextInput组件不支持所有道具,则最好明确地选择和传播道具:

const textInputFieldProps = {label: "my Name", required: true, notThis: "nono"};

const Comp = () => {
  // destructure all props for TextInput (here label and required)
  const { label, required, ...rest } = textInputFieldProps;
  // and spread them into TextInput
  return  <TextInput {...{ label, required }} />
};

You could go the other way and use the rest assignment for all needed props:你可以反其道而行之,并为所有需要的道具使用其余的分配:

const Comp2 = () => {
  const { notThis, ...rest } = textInputFieldProps;
  return  <TextInput {...rest} />
};

I would favor the first one, as even more explicit and safe in terms of props passed into TextInput .我更喜欢第一个,因为在传递给TextInput的道具方面更加明确和安全。 Imagine you add more props, these also would be propagated via rest , and you might not want this.想象一下,您添加了更多道具,这些道具也会通过rest传播,而您可能不希望这样。

What React docs say to spread attributes:React 文档所说的传播属性:

Spread attributes can be useful but they also make it easy to pass unnecessary props to components that don't care about them or to pass invalid HTML attributes to the DOM. Spread 属性可能很有用,但它们也使将不必要的 props 传递给不关心它们的组件或将无效的 HTML 属性传递给 DOM 变得容易。 We recommend using this syntax sparingly.我们建议谨慎使用此语法。

We can use the spread operator to achieve this.我们可以使用扩展运算符来实现这一点。 In general, the props object can contain two types of property: one is component-specific, other is our own.一般来说,props 对象可以包含两种类型的属性:一种是组件特定的,另一种是我们自己的。 I usually filter the properties that should not be in component props.我通常会过滤不应该出现在组件 props 中的属性。 Below is an example code.下面是一个示例代码。

 const { other1, other2, ...rest } = props <TextInput {...rest} style={[{color: defaultColor, borderColor: defaultColor}, styles.defaultTextInputStyle, textInputStyle]} onChangeText={text => onChangeHandler(text)} value={value} />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM