简体   繁体   English

在 Reactjs 中,我如何传递两个道具,其中一个是数组

[英]In Reactjs, how can i pass two props with one of them is array

I am trying to pass two props to a child component like this:我正在尝试将两个道具传递给这样的子组件:

<CountData onchoiceChange={[{value: 1, label: A}]} selectedGroup ={"GrpA"} /> 

And since onchoiceChange is an jsonarray it could be something like:由于 onchoiceChange 是一个 jsonarray 它可能是这样的:

[{value: 1, label: A},{value: 2, label: B}]

and if i pass props like the above, in my child functional component如果我通过像上面这样的道具,在我的子功能组件中

export default function CountData(onchoiceChange,selectedGroup) {

my value become我的价值变成

-onchoiceChange.onchoiceChange -onchoiceChange.onchoiceChange
-onchoiceChange.selectedGroup -onchoiceChange.selectedGroup

and my desired is only -onchoiceChange & -selectedGroup而我想要的只是 -onchoiceChange & -selectedGroup

thank if anyone can help谢谢是否有人可以提供帮助

Your component is receiving a single parameter, which is an object containing the props of your component.您的组件正在接收一个参数,这是一个 object 包含您的组件的道具。 Hence, you can destructure it like this, to access the object you want:因此,您可以像这样对其进行解构,以访问您想要的 object:

export default function CountData({onchoiceChange,selectedGroup}) { ... }

You can also destructure the props object in an other way, which is equivalent, more verbose, but easyer to understand:你也可以用另一种方式解构 props object,这是等效的,更详细,但更容易理解:

export default function CountData(props)   {
    const {onchoiceChange,selectedGroup} = props
}

If the way how props are working in React is not clear for you, I suggest you to read the official documentation如果你不清楚 React 中 props 的工作方式,我建议你阅读官方文档

And if you want to know more about destructuration, you can read this article for instance.如果你想了解更多关于解构的信息,你可以阅读这篇文章

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

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