如何在详细的道具React-Native中获取道具变量

问题描述 投票:0回答:2

我有这样的自定义组件

const MyCustomComponent = ({ value, style }) => {
   // I can access value & style with value and style
   return <View style={style}>
      <Text>{value}</Text>
   </View>
}

我可以这样称呼它

<MyCustomComponent value="123" style={{ color: "blue" }} />

我的问题是如何获取参数或alyelse,以将所有道具传递给我的组件?

在本机函数中,我可以使用arguments获取allProps作为数组并将其设置为新变量,例如const allProps = arguments[0]

箭头功能怎么样?

react-native ecmascript-6 arguments components arrow-functions
2个回答
0
投票
您是否要使用单个变量获取'值'和'样式'道具?

您可以使用:

const MyCustomComponent = (props) => { // I can access value & style with value and style return <View style={props.style}> <Text>{props.value}</Text> </View> }


0
投票
这里您拥有的是一个功能组件,它的构建方式使其仅接收一个对象-即道具。执行此操作时:const MyComponent({value, style})分解了prop对象,仅提取了两个变量。

您应该这样做:

© www.soinside.com 2019 - 2024. All rights reserved.