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 :: address format 
Javascript :: max value javascript 
Javascript :: flat function javascript 
Javascript :: javascript floating point addition 
Javascript :: Javascript using for loop to loop through an array 
Javascript :: horizontal scrollview in react js 
Javascript :: javscript assert 
Javascript :: Reactjs function exemple useEffect 
Javascript :: gif as animation react 
Javascript :: node fetch 
Javascript :: nodejs mysql query 
Javascript :: mongodb findoneandupdate return new document 
Javascript :: extends vs includes use case 
Javascript :: discord js embed footer 
Javascript :: vs code jsconfig 
Javascript :: pass text and variable in alert javascript 
Javascript :: angular ngstyle variable 
Javascript :: checked unchecked through js 
Javascript :: js range similar to python 
Javascript :: ready function jq 
Javascript :: iconify react 
Javascript :: Add jquery in extension 
Javascript :: Loading "cdnify.js" tasks...ERROR 
Javascript :: Javascript Unordered List HTML form Array 
Javascript :: preview multiple image before upload 
Javascript :: change profile photo with javascript 
Javascript :: flask vue.js not working 
Javascript :: custom indicator js tradingview 
Javascript :: bootstrap 4 form validator with jquery 
Javascript :: JavaScript max 32-bit integer 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =