Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
153
rated 0 times [  157] [ 4]  / answers: 1 / hits: 9671  / 3 Years ago, sat, february 27, 2021, 12:00:00

Is there any way where I can pass in multiple values in a single prop?


Right now how it works is I have one component and it takes in lots of values like this:


<div>
<MyComponent
valueA={100}
valueB={90}
valueC={80}
valueD={70}
valueE={60}
/>
</div>


Here for example A and B are related and I want to pass them together like


<div>
<MyComponent
valueAB={100, 90}
valueC={80}
valueD={70}
valueE={60}
/>
</div>

It probably needs to be in a single object like this: valueAB={{100, 90}} but I have no idea.


How can I do this and extract both of the values on the other side?


More From » html

 Answers
5

You can pass an array or an object:


// e.g
<MyComponent
valueAB={[100, 90]}
valueC={{ a: 100, b: 90}} />

And in MyComponent you will get valueAB prop
as array, hence you can do something like:


props.valueAB[0] // 100

Or for objet : props.valueC.a is also 100 .


[#1722] Monday, February 22, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
makaylahk

Total Points: 166
Total Questions: 94
Total Answers: 117

Location: Gabon
Member since Sat, Jul 25, 2020
4 Years ago
;