[React] React props
props
- props๋ ์์ ์ปดํฌ๋ํธ์์ ์ ๋ฌํด ์ค ๊ฐ
- Component ์์ฑ์ผ๋ก Key = Value ๋ช ์ํ๋ฉด Key, Value ๊ฐ props๋ ๊ฐ์ฒด๋ก ์ ๋ฌ
์์ ์ปดํฌ๋ํธ
import logo from './logo.svg';
import './App.css';
import Profile from './components/Profile';
function AppProfile() {
return (
<>
<Profile
image='https://images.unsplash.com/photo-1580489944761-15a19d654956?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=922&q=80'
name='kim'
title='ํ๋ก ํธ ์๋ ๊ฐ๋ฐ์'
isNew={false}
/>
</>
);
}
export default AppProfile;
ํ์ ์ปดํฌ๋ํธ
import React from 'react';
export default function Profile(props) {
return (
<div className='profile'>
<img className='photo' src={props.image} alt='avatar' />
{isNew && <span className='new'>New</span>}
<h1>{props.name}</h1>
<p>{props.title}</p>
</div>
);
}
* props ๋ถ๋ถ์ JS object deconstructing ์ด์ฉ๋ ๊ฐ๋ฅ!
import React from 'react';
export default function Profile({ image, name, title, isNew }) {
return (
<div className='profile'>
<img className='photo' src={image} alt='avatar' />
{props.isNew && <span className='new'>New</span>}
<h1>{name}</h1>
<p>{title}</p>
</div>
);
}
Leave a comment