Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

pass object as props

interface EmployeeProps {
  name: string;
  age: number;
  country: string;
}

function Employee({name, age, country}: EmployeeProps) {
  return (
    <div>
      <h2>{name}</h2>
      <h2>{age}</h2>
      <h2>{country}</h2>
    </div>
  );
}

export default function App() {
  const obj = {name: 'Alice', age: 29, country: 'Austria'};

  return (
    <div>
      <Employee {...obj} />
    </div>
  );
}
Comment

react pass object as props

Use the "spread" operator {... }

Usage: <MyJsx {...commonProps} />
Comment

Passing objects as Props in react

const UserCard = props => {
  const name = props.user.name
  const role = props.user.role
  const age = props.user.age
  const profilePic = props.user.profilePic
  return (
    <div>
      <p>Name: {name}</p>
      <p>Role: {role}</p>
      <p>Age: {age}</p>
      <img src={profilePic} alt={name} />
    </div>
  )
}

function App() {
  const user = {
    name: "Ranjeet",
    role: "WordPress Developer",
    age: 27,
    profilePic: "image.jpg",
  }

  return (
    <div>
      <UserCard user={user} />
    </div>
  )
}

export default App
Comment

PREVIOUS NEXT
Code Example
Javascript :: electronjs 
Javascript :: browserrouter invalid hook call 
Javascript :: object destructuring example 
Javascript :: add to a js object 
Javascript :: nextjs local storage 
Javascript :: amount into words 
Javascript :: toast js 
Javascript :: how to add attribute in a element 
Javascript :: js csv to json 
Javascript :: get number right of the dot length javascript 
Javascript :: row auto textarea 
Javascript :: array within array javascript 
Javascript :: hashnode 
Javascript :: js list random order 
Javascript :: sort array descending 
Javascript :: js string includes count 
Javascript :: decode jwt token without key 
Javascript :: how to fetch data redux 
Javascript :: javascript even number 
Javascript :: how to sent react from data in mongodb 
Javascript :: json in python 
Javascript :: append item to array javascript 
Javascript :: how to install react js 
Javascript :: js value to boolean 
Javascript :: double exclamation mark js 
Javascript :: create javascript for loop 
Javascript :: reducer react 
Javascript :: get html 
Javascript :: onkeypress 
Javascript :: factory function vs constructor javascript 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =