158

I have an object contains multiple common key-value props, that I want to pass on to some jsx. Something like this:

const commonProps = {myProp1: 'prop1',myProp2: 'prop2'}; <MyJsx commonProps /> 

I want this to function as passing individual props:

<MyJsx myProp1={commonProps.myProp1} myProp2={commonProps.myProp2}/> 

Is this possible?

2

9 Answers 9

262

Is this possible?

Yes its possible, but the way you are sending it is not correct.

The meaning of <MyJsx commonProps /> is:

<MyJsx commonProps={true} /> 

So if you don't specify any value, by default it will take true. To pass the object, you need to write it like this:

const commonProps = {myProp1: 'prop1',myProp2: 'prop2'}; <MyJsx commonProps={commonProps} /> 

Update:

If you have an object and want to pass all the properties as separate prop, write it like this:

<MyJsx {...commonProps} /> 
Sign up to request clarification or add additional context in comments.

6 Comments

This is not what I need. I edited my question so it will be more clear. I need the props to be sent as individual props
updated the answer, please check. Write it like this: <MyJsx {...commonProps} />
just what I needed
Yes! Else I ended up doing things like <p>{props.props.hello}</p>
What about a single value? Instead of <Component prop1={prop1} />, something like <Component {...prop1} /> Can't get it working.
|
85

You can use the spread operator to do this.

You can simply do <MyJsx {...commonProps} />

Now what all individual properties you have in commonProps will be sent as individual props to MyJsx.

2 Comments

This trick allowed me to switch 10 lines of code for 1. Much appreciated!
19

You need to use double braces, like this:

messages={{tile:'Message 1'}} 

Or to pass many objects:

messages={[{title:'Message 1', name: 'Andrew'}, {title:'Message 2', name: 'Greg'}]} 

It's simply JS syntax inside braces.

A final component might look like this

<Chat title="Some str" messages={[{title:'Message 1', name: 'Andrew'}, {title:'Message 2', name: 'Greg'}]} /> 

Comments

11

You can pass props as object in two ways:-

const commonProps = {myProp1: 'prop1',myProp2: 'prop2'};

1.Using spread Operator:-

<MyJsx {...commonProps} /> 

2.Simply pass that props:-

<MyJsx commonProps = {commonProps ? commonProps : true} /> 

Comments

1

Can also do something like this:

<MyJsx objectProps={{ prop1, prop2 }}/> 

This way you don't have to write prop1: prop1 (if it is a variable or function such as state setState).

It can then be accessed in the component using destructuring: let { prop1, prop2 } = props.objectProps

Comments

1

I had for some object types errors in typescript. as const helped here.

const myprops = { YOUR OBJECT } as const 

Comments

0
const object={name:'xyz',age:50} 

use double braces as follows

<Message {{object}}/> 

Child

function Message({object}){ //You can get object here.// } 

Comments

0

React can not Render Object! Sounds Confusing, Right?

Long Story Short: (TL; DR) Browsers primarily understand HTML, CSS, and JavaScript. When you pass something (except object) into props, JSX automatically converts it into a renderable format and updates the virtual DOM (because it's faster than direct DOM updates). But when you try to pass a raw object directly into JSX, React gets confused and can’t interpret it properly — it's like double objects for React.

So, what is the solution? ➡️ Convert the object into a string using JSON.stringify(). OR ➡️ Treat the object as a nested object by accessing its properties.

Example:

const Person = {myName: "Emran Khan"} return ( <main> {JSON.stringify(Person)} <br /> {Person.myName} </main> ) 

Comments

0

To make the image square (even if the image's intrinsic dimensions/aspect ratio is not square), you can give the image the following declaration:

  • aspect-ratio: square;

To make the image not get distorted/stretched, you can add the following style:

  • object-fit: cover;

Hope this helps!

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.