function Greeting(props) {
const isLoggedIn = props.isLoggedIn;
if (isLoggedIn) {
return <UserGreeting />;
}
return <GuestGreeting />;
}
function Greeting(props) {
return isLoggedIn ? <UserGreeting /> : <GuestGreeting />;
}
const App = () => {
return (
<>
<div>{[false, null, undefined, true]}</div>
<div>{false}</div>
<div>{null}</div>
<div>{undefined}</div>
<div>{true}</div> 안의 값들은 화면에 render되지 않음
</>
);
}
최적화를 위해 key값이 겹치지 않도록 attribute를 부여하면 error를 해결할 수 있고, side effect를 방지할 수 있다.
1~100까지 들어있는 array가 있을때, '7의 배수'라는 텍스트를 포함한 button 출력
10의 배수인 경우 출력 x
그 외 숫자가 들어있는 button 출력하기
import React from 'react';
// 1부터 100까지 들어있는 arr
const arr = Array.from(Array(100), (_, i) => i+1);
const App = () => {
return (
<div>
{/* fill here */}
</div>
);
};
export default App;
import React from 'react';
// 1부터 100까지 들어있는 arr
const arr = Array.from(Array(100), (_, i) => i+1);
const App = (props) => {
return (
<div>
{props.arr.map((item, idx) => {
if (item % 7 === 0) {
return (
<button type='button' key={idx}>
7의 배수
</button>
);
} else if (item % 10 === 0) {
return null;
} else {
return (
<button type='button' key={idx}>
{item}
</button>
);
}
})}
</div>
);
};
export default App;
1~100이 들어있는 button 100개를 만들고 버튼을 클릭하면 들어있는 숫자를 alert로 띄워주기
import React from 'react';
// 1부터 100까지 들어있는 arr
const arr = Array.from(Array(100), (_, i) => i+1);
const App = () => {
return (
<div>
{arr.map(item => (
<button
type='button'
key={item}
onClick={() => {alert(item);}}
>
{item}
</button>
))}
</div>
);
};
export default App;
App 컴포넌트에는 key추가, Card 컴포넌트 채워넣기
import React from 'react';
const users = [{
name: 'kim',
id: 5,
}, {
name: 'hello',
id: 6,
}, {
name: 'jin',
id: 7,
}, {name: 'hi',
id: 10,}, {
name: 'yellow',
id: 8,
}];
const Card = (props) => {
return (
<div>
</div>
);
}
const App = () => {
return (
<>
{users.map((user) => <Card user={user} />)}
</>
);
};
export default App;
const Card = (props) => {
return (
<div>
id: {props.user.id}<br /> JS영역이기에 {}사용
name: {props.user.name}
</div>
);
}
const App = () => {
return (
<>
{users.map((user) => <Card user={user} key={user.id} />)}
</>
);
};