JSX ์์ด ์ฌ์ฉํ๋ React
These docs are old and wonโt be updated. Go to react.dev for the new React docs.
React๋ฅผ ์ฌ์ฉํ ๋ JSX๋ ํ์๊ฐ ์๋๋๋ค. ๋น๋ ํ๊ฒฝ์์ ์ปดํ์ผ ์ค์ ์ ํ๊ณ ์ถ์ง ์์ ๋ JSX ์์ด React๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ํนํ ํธ๋ฆฌํฉ๋๋ค.
๊ฐ JSX ์๋ฆฌ๋จผํธ๋ React.createElement(component, props, ...children)
๋ฅผ ํธ์ถํ๊ธฐ ์ํ ๋ฌธ๋ฒ ์คํ์
๋๋ค. ๊ทธ๋์ JSX๋ก ํ ์ ์๋ ๋ชจ๋ ๊ฒ์ ์์ JavaScript๋ก๋ ํ ์ ์์ต๋๋ค.
์๋ฅผ ๋ค์ด ๋ค์์ JSX๋ก ์์ฑ๋ ์ฝ๋๋
class Hello extends React.Component {
render() {
return <div>Hello {this.props.toWhat}</div>;
}
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Hello toWhat="World" />);
์๋์ฒ๋ผ JSX๋ฅผ ์ฌ์ฉํ์ง ์์ ์ฝ๋๋ก ์ปดํ์ผ๋ ์ ์์ต๋๋ค.
class Hello extends React.Component {
render() {
return React.createElement('div', null, `Hello ${this.props.toWhat}`);
}
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(React.createElement(Hello, {toWhat: 'World'}, null));
JSX๊ฐ JavaScript๋ก ๋ณํ๋๋ ์์๋ฅผ ๋ ๋ณด๊ณ ์ถ๋ค๋ฉด the online Babel compiler๋ฅผ ์ฐธ๊ณ ํ์ธ์.
์ปดํฌ๋ํธ๋ ๋ฌธ์์ด์ด๋ React.Component
์ ํ์ ํด๋์ค ๋๋ ์ปดํฌ๋ํธ๋ฅผ ์ํ ์ผ๋ฐ ํจ์๋ก ์ ๊ณต๋ฉ๋๋ค.
React.createElement
๋ฅผ ๋๋ฌด ๋ง์ด ์
๋ ฅํ๋ ๊ฒ์ด ํผ๊ณคํ๋ค๋ฉด ์งง์ ๋ณ์์ ํ ๋นํ๋ ๋ฐฉ๋ฒ์ด ์์ต๋๋ค.
const e = React.createElement;
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(e('div', null, 'Hello World'));
React.createElement
๋ฅผ ์งง์ ๋ณ์์ ํ ๋นํ๋ฉด ํธ๋ฆฌํ๊ฒ JSX ์์ด React๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
๋ ๊ฐ๊ฒฐํ ๊ตฌ๋ฌธ์ ์ ๊ณตํ๋ react-hyperscript
๋ hyperscript-helpers
๊ฐ์ ์ปค๋ฎค๋ํฐ ํ๋ก์ ํธ๋ฅผ ์ฐธ๊ณ ํด๋ ์ข์ต๋๋ค.