์กฐ๊ฑด๋ถ ๋ ๋๋ง
These docs are old and wonโt be updated. Go to react.dev for the new React docs.
These new documentation pages teach modern React and include live examples:
React์์๋ ์ํ๋ ๋์์ ์บก์ํํ๋ ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค ์ ์์ต๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด ์ ํ๋ฆฌ์ผ์ด์ ์ ์ํ์ ๋ฐ๋ผ์ ์ปดํฌ๋ํธ ์ค ๋ช ๊ฐ๋ง์ ๋ ๋๋งํ ์ ์์ต๋๋ค.
React์์ ์กฐ๊ฑด๋ถ ๋ ๋๋ง์ JavaScript์์์ ์กฐ๊ฑด ์ฒ๋ฆฌ์ ๊ฐ์ด ๋์ํฉ๋๋ค. if
๋ ์กฐ๊ฑด๋ถ ์ฐ์ฐ์
์ ๊ฐ์ JavaScript ์ฐ์ฐ์๋ฅผ ํ์ฌ ์ํ๋ฅผ ๋ํ๋ด๋ ์๋ฆฌ๋จผํธ๋ฅผ ๋ง๋๋ ๋ฐ์ ์ฌ์ฉํ์ธ์. ๊ทธ๋ฌ๋ฉด React๋ ํ์ฌ ์ํ์ ๋ง๊ฒ UI๋ฅผ ์
๋ฐ์ดํธํ ๊ฒ์
๋๋ค.
์๋ ๋ ์ปดํฌ๋ํธ๊ฐ ์๋ค๊ณ ๊ฐ์ ํด ๋ด ์๋ค.
function UserGreeting(props) {
return <h1>Welcome back!</h1>;
}
function GuestGreeting(props) {
return <h1>Please sign up.</h1>;
}
์ด์ ์ฌ์ฉ์์ ๋ก๊ทธ์ธ ์ํ์ ๋ง๊ฒ ์ ์ปดํฌ๋ํธ ์ค ํ๋๋ฅผ ๋ณด์ฌ์ฃผ๋ Greeting
์ปดํฌ๋ํธ๋ฅผ ๋ง๋ญ๋๋ค.
function Greeting(props) {
const isLoggedIn = props.isLoggedIn;
if (isLoggedIn) { return <UserGreeting />; } return <GuestGreeting />;}
const root = ReactDOM.createRoot(document.getElementById('root'));
// Try changing to isLoggedIn={true}:
root.render(<Greeting isLoggedIn={false} />);
์ด ์์๋ isLoggedIn
prop์ ๋ฐ๋ผ์ ๋ค๋ฅธ ์ธ์ฌ๋ง์ ๋ ๋๋ง ํฉ๋๋ค.
์๋ฆฌ๋จผํธ ๋ณ์
์๋ฆฌ๋จผํธ๋ฅผ ์ ์ฅํ๊ธฐ ์ํด ๋ณ์๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ถ๋ ฅ์ ๋ค๋ฅธ ๋ถ๋ถ์ ๋ณํ์ง ์์ ์ฑ๋ก ์ปดํฌ๋ํธ์ ์ผ๋ถ๋ฅผ ์กฐ๊ฑด๋ถ๋ก ๋ ๋๋ง ํ ์ ์์ต๋๋ค.
๋ก๊ทธ์์๊ณผ ๋ก๊ทธ์ธ ๋ฒํผ์ ๋ํ๋ด๋ ๋ ์ปดํฌ๋ํธ๊ฐ ์๋ค๊ณ ๊ฐ์ ํด ๋ณด์ธ์.
function LoginButton(props) {
return (
<button onClick={props.onClick}>
Login
</button>
);
}
function LogoutButton(props) {
return (
<button onClick={props.onClick}>
Logout
</button>
);
}
์๋์ ์์์์๋ LoginControl
์ด๋ผ๋ ์ ์ํ ์ปดํฌ๋ํธ ๋ฅผ ๋ง๋ค ๊ฒ์
๋๋ค.
์ด ์ปดํฌ๋ํธ๋ ํ์ฌ ์ํ์ ๋ง๊ฒ <LoginButton />
์ด๋ <LogoutButton />
์ ๋ ๋๋งํฉ๋๋ค. ๋ํ ์ด์ ์์์์์ <Greeting />
๋ ํจ๊ป ๋ ๋๋งํฉ๋๋ค.
class LoginControl extends React.Component {
constructor(props) {
super(props);
this.handleLoginClick = this.handleLoginClick.bind(this);
this.handleLogoutClick = this.handleLogoutClick.bind(this);
this.state = {isLoggedIn: false};
}
handleLoginClick() {
this.setState({isLoggedIn: true});
}
handleLogoutClick() {
this.setState({isLoggedIn: false});
}
render() {
const isLoggedIn = this.state.isLoggedIn;
let button;
if (isLoggedIn) { button = <LogoutButton onClick={this.handleLogoutClick} />; } else { button = <LoginButton onClick={this.handleLoginClick} />; }
return (
<div>
<Greeting isLoggedIn={isLoggedIn} /> {button} </div>
);
}
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<LoginControl />);
๋ณ์๋ฅผ ์ ์ธํ๊ณ if
๋ฅผ ์ฌ์ฉํด์ ์กฐ๊ฑด๋ถ๋ก ๋ ๋๋ง ํ๋ ๊ฒ์ ์ข์ ๋ฐฉ๋ฒ์ด์ง๋ง ๋ ์งง์ ๊ตฌ๋ฌธ์ ์ฌ์ฉํ๊ณ ์ถ์ ๋๊ฐ ์์ ์ ์์ต๋๋ค. ์ฌ๋ฌ ์กฐ๊ฑด์ JSX ์์์ ์ธ๋ผ์ธ(inline)์ผ๋ก ์ฒ๋ฆฌํ ๋ฐฉ๋ฒ ๋ช ๊ฐ์ง๋ฅผ ์๋์์ ์๊ฐํ๊ฒ ์ต๋๋ค.
๋ ผ๋ฆฌ && ์ฐ์ฐ์๋ก If๋ฅผ ์ธ๋ผ์ธ์ผ๋ก ํํํ๊ธฐ
JSX ์์๋ ์ค๊ดํธ๋ฅผ ์ด์ฉํด์ ํํ์์ ํฌํจ ํ ์ ์์ต๋๋ค. ๊ทธ ์์ JavaScript์ ๋
ผ๋ฆฌ ์ฐ์ฐ์ &&
๋ฅผ ์ฌ์ฉํ๋ฉด ์ฝ๊ฒ ์๋ฆฌ๋จผํธ๋ฅผ ์กฐ๊ฑด๋ถ๋ก ๋ฃ์ ์ ์์ต๋๋ค.
function Mailbox(props) {
const unreadMessages = props.unreadMessages;
return (
<div>
<h1>Hello!</h1>
{unreadMessages.length > 0 && <h2> You have {unreadMessages.length} unread messages. </h2> } </div>
);
}
const messages = ['React', 'Re: React', 'Re:Re: React'];
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Mailbox unreadMessages={messages} />);
JavaScript์์ true && expression
์ ํญ์ expression
์ผ๋ก ํ๊ฐ๋๊ณ false && expression
์ ํญ์ false
๋ก ํ๊ฐ๋ฉ๋๋ค.
๋ฐ๋ผ์ &&
๋ค์ ์๋ฆฌ๋จผํธ๋ ์กฐ๊ฑด์ด true
์ผ๋ ์ถ๋ ฅ์ด ๋ฉ๋๋ค. ์กฐ๊ฑด์ด false
๋ผ๋ฉด React๋ ๋ฌด์ํ๊ณ ๊ฑด๋๋๋๋ค.
falsy ํํ์์ ๋ฐํํ๋ฉด ์ฌ์ ํ &&
๋ค์ ์๋ ํํ์์ ๊ฑด๋๋ฐ์ง๋ง falsy ํํ์์ด ๋ฐํ๋๋ค๋ ๊ฒ์ ์ฃผ์ํด์ฃผ์ธ์. ์๋ ์์์์, <div>0</div>
์ด render ๋ฉ์๋์์ ๋ฐํ๋ฉ๋๋ค.
render() {
const count = 0; return (
<div>
{count && <h1>Messages: {count}</h1>} </div>
);
}
์กฐ๊ฑด๋ถ ์ฐ์ฐ์๋ก If-Else๊ตฌ๋ฌธ ์ธ๋ผ์ธ์ผ๋ก ํํํ๊ธฐ
์๋ฆฌ๋จผํธ๋ฅผ ์กฐ๊ฑด๋ถ๋ก ๋ ๋๋งํ๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ์กฐ๊ฑด๋ถ ์ฐ์ฐ์์ธ condition ? true: false
๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์
๋๋ค.
์๋์ ์์์์๋ ์งง์ ๊ตฌ๋ฌธ์ ์กฐ๊ฑด๋ถ๋ก ๋ ๋๋งํฉ๋๋ค.
render() {
const isLoggedIn = this.state.isLoggedIn;
return (
<div>
The user is <b>{isLoggedIn ? 'currently' : 'not'}</b> logged in. </div>
);
}
๊ฐ๋ ์ฑ์ ์ข ๋จ์ด์ง์ง๋ง, ๋ ํฐ ํํ์์๋ ์ด ๊ตฌ๋ฌธ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
render() {
const isLoggedIn = this.state.isLoggedIn;
return (
<div>
{isLoggedIn ? <LogoutButton onClick={this.handleLogoutClick} />
: <LoginButton onClick={this.handleLoginClick} /> }
</div> );
}
JavaScript์ ๋ง์ฐฌ๊ฐ์ง๋ก, ๊ฐ๋ ์ฑ์ด ์ข๋ค๊ณ ์๊ฐํ๋ ๋ฐฉ์์ ์ ํํ๋ฉด ๋ฉ๋๋ค. ๋ํ ์กฐ๊ฑด์ด ๋๋ฌด ๋ณต์กํ๋ค๋ฉด ์ปดํฌ๋ํธ๋ฅผ ๋ถ๋ฆฌํ๊ธฐ ์ข์ ๋ ์ผ ์๋ ์๋ค๋ ๊ฒ์ ๊ธฐ์ตํ์ธ์.
์ปดํฌ๋ํธ๊ฐ ๋ ๋๋งํ๋ ๊ฒ์ ๋ง๊ธฐ
๊ฐ๋ ๋ค๋ฅธ ์ปดํฌ๋ํธ์ ์ํด ๋ ๋๋ง๋ ๋ ์ปดํฌ๋ํธ ์์ฒด๋ฅผ ์จ๊ธฐ๊ณ ์ถ์ ๋๊ฐ ์์ ์ ์์ต๋๋ค. ์ด๋๋ ๋ ๋๋ง ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ ๋์ null
์ ๋ฐํํ๋ฉด ํด๊ฒฐํ ์ ์์ต๋๋ค.
์๋์ ์์์์๋ <WarningBanner />
๊ฐ warn
prop์ ๊ฐ์ ์ํด์ ๋ ๋๋ง๋ฉ๋๋ค. prop์ด false
๋ผ๋ฉด ์ปดํฌ๋ํธ๋ ๋ ๋๋งํ์ง ์๊ฒ ๋ฉ๋๋ค.
function WarningBanner(props) {
if (!props.warn) { return null; }
return (
<div className="warning">
Warning!
</div>
);
}
class Page extends React.Component {
constructor(props) {
super(props);
this.state = {showWarning: true};
this.handleToggleClick = this.handleToggleClick.bind(this);
}
handleToggleClick() {
this.setState(state => ({
showWarning: !state.showWarning
}));
}
render() {
return (
<div>
<WarningBanner warn={this.state.showWarning} /> <button onClick={this.handleToggleClick}>
{this.state.showWarning ? 'Hide' : 'Show'}
</button>
</div>
);
}
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Page />);
์ปดํฌ๋ํธ์ render
๋ฉ์๋๋ก๋ถํฐ null
์ ๋ฐํํ๋ ๊ฒ์ ์๋ช
์ฃผ๊ธฐ ๋ฉ์๋ ํธ์ถ์ ์ํฅ์ ์ฃผ์ง ์์ต๋๋ค. ๊ทธ ์๋ก componentDidUpdate
๋ ๊ณ์ํด์ ํธ์ถ๋๊ฒ ๋ฉ๋๋ค.