function Profile(props) {
return (
<div className="card">
<Avatar {...props} />
</div>
);
}
if (isPacked) {
return null;
}
return <li className="item">{name}</li>;
messageCount && <p>New messages</p>
이렇게 하면 안됨. 0을 렌더링 해버림. !!messageCount 는 가능.import { Fragment } from 'react';
// ...
const listItems = people.map(person =>
<Fragment key={person.id}>
<h1>{person.name}</h1>
<p>{person.bio}</p>
</Fragment>
);
let guest = 0;
function Cup() {
// Bad: changing a preexisting variable!
// 나쁨: 기존 변수를 변경합니다!
guest = guest + 1;
return <h2>Tea cup for guest #{guest}</h2>;
}
export default function TeaSet() {
return (
<>
<Cup />
<Cup />
<Cup />
</>
);
}
console.log(count); // 0
setCount(count + 1); // Request a re-render with 1
console.log(count); // Still 0!
setNumber(0 + 1);
setNumber(0 + 1);
setNumber(0 + 1);
<button onClick={() => {
setNumber(n => n + 1);
setNumber(n => n + 1);
setNumber(n => n + 1);
}}>+3</button>
<button onClick={() => {
setNumber(number + 1);
setNumber(number + 1);
setNumber(number + 1);
}}>+3</button>
https://codesandbox.io/s/es6uix?file=%2FprocessQueue.js&utm_medium=sandpack
export function getFinalState(baseState, queue) {
let finalState = 0;
for (let update of queue) {
if (typeof update === "function") {
finalState = update(finalState);
} else {
finalState = update;
}
}
return finalState;
}
import Image from './Image.js';
import { createRoot } from 'react-dom/client';
const root = createRoot(document.getElementById('root'))
root.render(<Image />);
렌더링이 촉발되면 React는 컴포넌트를 호출하여 화면에 표시할 내용을 파악함
이떄 컴포넌트가 다른 컴포넌트를 반환하면(즉 부모 컴포넌트이면) 함께 여러번 렌더링됨