79234653

Date: 2024-11-28 16:23:37
Score: 0.5
Natty:
Report link

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>
)
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: Emran Khan