-
[React ๊ณต์๋ฌธ์ ์ฝ๊ธฐ] Main Concepts 2. Introducing JSXweb/React&Redux 2020. 2. 15. 23:54
Introducing JSX
const element = <h1>Hello, world!</h1>
๐ ์๋ฐ์คํฌ๋ฆฝํธ์ HTML์ด ๋ง๊ตฌ ๋ค์์ฌ ์๋ ๋ฏํ ์์ํ ๊ตฌ๋ฌธ์ฒ๋ผ ๋ณด์ด์ง๋ง
์ด ๋์ JSX๋ค.JSX๋ JavaScript์ ํ์ฅ ๋ฌธ๋ฒ์ด๋ฉฐ
๋ฆฌ์กํธ์ UI๋ฅผ ๋ฌ์ฌํ๋ ๋ฐ ์ฌ์ฉ๋๋ค.
template language๋ฅผ ์๊ฐ๋๊ฒ ํ์ง๋ง JavaScript์
โปํ ํ๋ฆฟ ์ธ์ด๋?
JSX๋ React "elements"๋ฅผ ๋ง๋ ๋ค.
JSX์ ๋ํด ์ข ๋ ์์ธํ ์์๋ณด์.
Why JSX?
์ฐ์ , JSX๊ฐ ๋ฆฌ์กํธ๋ฅผ ์ฌ์ฉํ๋ ํ์์กฐ๊ฑด์ ์๋๋ค.
๋ฆฌ์กํธ ์ด์ ์ ํด๋ผ์ด์ธํธ๋ฅผ ๊ตฌ์ฑํ๋ ๋ฐฉ๋ฒ
์ฆ, HTML๊ณผ JavaScript๋ฅผ ๋ฐ๋ก ์์ฑํ๊ณ <script>ํ๊ทธ๋ก ์ฐ๊ฒฐํ๋ ๋ฐฉ๋ฒ์
๋งํฌ์ (markup)๊ณผ ๋ก์ง์ ์๋ก ๋ค๋ฅธ ํ์ผ์ ์์ฑํ๊ธฐ ๋๋ฌธ์ ๊ธฐ์ ์ ์ธ์์ ์ผ๋ก ๋ถ๋ฆฌํ๋ค.
์ด๋ค ์๋ฆฌ๋จผํธ์ ์ด๋ค ํจ์๊ฐ ์ฐ๊ฒฐ๋์ด ์๋์ง ํ ๋์ ์์๋ณด๊ธฐ ์ด๋ ต๋ค.
๋ฐ๋ฉด์
๋ฆฌ์กํธ๋ ๋งํฌ์ ๊ณผ ๋ก์ง์ ๋ชจ๋ ํฌํจํ๊ณ ์๋ ์ ๋์ธ ์ปดํฌ๋ํธ(Component)๋ฅผ ์ฌ์ฉํ๋ค.
๋ฆฌ์กํธ๋ ๋ ๋๋ง ๋ก์ง์ด ๋ค๋ฅธ UI๋ก์ง
(์ด๋ป๊ฒ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํ๋์ง, ์ํ๊ฐ ๋ณ๊ฒฝ๋๋์ง, ๋ฐ์ดํฐ๊ฐ ํ์๋๊ธฐ ์ ์ค๋น๋๋์ง)
๊ณผ ์ฐ๊ฒฐ๋์ด ์๋ค.
JSX๋ JS์ฝ๋ ๋ด์์ UI๋ชจ์ต์ ์ง๊ด์ ์ผ๋ก ๋ณด์ฌ์ค๋ค.
JSX๊ฐ ํ์๋ ์๋์ง๋ง ๋ฆฌ์กํธ๊ฐ ๋ ์ ์ฉํ ์๋ฌ, ๊ฒฝ๊ณ ๋ฉ์ธ์ง๋ฅผ ํ์ํ ์ ์๊ฒ ํ๋ค.
Embedding Expressions in JSX
Q. JSX ์์์ ๋ณ์๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด?
const name = 'Josh Perez'; const element = <h1>Hello, {name}</h1>; // ์ด๊ฒ JSX๋ค ReactDOM.render( element, document.getElementById('root') );
A. { }(์ค๊ดํธ)๋ก ๋ฌถ์ด์ค๋ค.
์ ์ฝ๋๋ id๊ฐ root์ธ ์๋ฆฌ๋จผํธ์ <h1>Hello, Josh Perez</h1>๋ฅผ ๋ ๋ํ๋ค.๋ณ์ ๋ฟ ์๋๋ผ ์๋ฐ์คํฌ๋ฆฝํธ ํํ(expression)์ด๋ผ๋ฉด ์ค๊ดํธ๋ก ๋ฌถ์ด JSX์์ ์ฌ์ฉํ ์ ์๋ค.
function formatName(user) { return user.firstName + ' ' + user.lastName; } const user = { firstName: 'Harper', lastName: 'Perez' }; const element = ( <h1> Hello, {formatName(user)}! </h1> ); ReactDOM.render( element, document.getElementById('root') );
๐ ํจ์๋ ์คํ ๊ฐ๋ฅ~
์ ์ฝ๋๋ id๊ฐ root์ธ ์๋ฆฌ์ <h1>Hello, Harper Perez</h1>๋ฅผ ๋ ๋ํ๋ค.๊ฐ๋ ์ฑ์ ์ํด JSX๋ฅผ ์ฌ๋ฌ ์ค๋ก ๋๋ ๋,
์๋ ;(์ธ๋ฏธ์ฝ๋ก ) ์ฝ์ ์ ํผํ๊ธฐ ์ํด ( ) ๊ดํธ๋ก ๋ฌถ์ด ์ฃผ๋ฉด ์ข๋ค.
JSX๋ expression์ด๋ค
์ปดํ์ผ ํ์ JSX๋ ์ผ๋ฐ์ ์ธ ์๋ฐ์คํฌ๋ฆฝํธ ํจ์ ํธ์ถ์ด ๋๊ณ , ๊ฐ์ฒด๋ก ํ๊ฐ๋๋ค.
if๋ฌธ๊ณผ for๋ฌธ ์์์ JSX๋ฅผ ์ฌ์ฉํ ์ ์๊ณ ,
JSX๋ฅผ ๋ณ์์ ํ ๋น, ์ธ์(argument)๋ก ์ฌ์ฉํ๊ณ ๋ ํจ์์ ๋ฆฌํด๊ฐ์ผ๋ก๋ ์ฌ์ฉํ ์ ์๋ค.function getGreeting(user) { if (user) { return <h1>Hello, {formatName(user)}!</h1>; } return <h1>Hello, Stranger.</h1>; }
JSX ์์ฑ ์ง์
์์ฑ ์ง์ ํ ๋ "" ๋ฐ์ดํ๋ก ๊ฐ์ธ๋๋ฐ
const element = <div tabIndex="0"></div>;
{ } curly braces ์ค๊ดํธ๋ฅผ ์ฌ์ฉํด์ ์๋ฐ์คํฌ๋ฆฝํธ expression์ ์์ฑ์ผ๋ก ๋ฃ์ด์ค ์๋ ์๋ค.
const element = <img src={user.avatarUrl}></img>;
๋ฌธ์์ด์ ๋ฃ๊ณ ์ถ์ผ๋ฉด ๋ฐ์ดํ ์ฐ๊ณ , expression์ ๋ฃ๊ณ ์ถ์ผ๋ฉด ์ค๊ดํธ๋ฅผ ์ด๋ค.
ํ๋์ ์์ฑ์ ๋ ๊ฐ์ ๊ฐ์ ๋ฃ์ ์๋ ์๋ค.
์ฃผ์
JSX๋ HTML๋ณด๋จ JavaScript์ ๋ ๊ฐ๊น๊ธฐ ๋๋ฌธ์,
React DOM์ ์นด๋ฉ์ผ์ด์ค(camelCase)๋ก ์์ฑ ์ด๋ฆ์ ์ง๋๋ค.
์) JSX์์๋ class๋ฅผ className์ผ๋ก ์ฌ์ฉ, tabindex -> tabIndex
Specifying Children with JSX
XML์ฒ๋ผ ๋ฐ๋ก ๋ซ์๋ ๋๋ค.
const element = <img src={user.avatarUrl} />;
JSX๋ children์ ๊ฐ์ง ์ ์๋ค.
const element = ( <div> <h1>Hello!</h1> <h2>Good to see you here.</h2> </div> );
JSX Prevents Injection Attacks
์ฌ์ฉ์ input์ JSX์ ๋ฃ๋ ๊ฒ์ ์์ ํ๋ค.
const title = response.potentiallyMaliciousInput; // This is safe: const element = <h1>{title}</h1>;
๊ธฐ๋ณธ์ ์ผ๋ก React DOM ์ JSX์ ํฌํจ ๋ ๊ฐ์ ๋ ๋๋งํ๊ธฐ ์ ์ ์ด์ค์ผ์ดํ ํ๋ค.
(escapeํ๋ ๊ฒ ๋ญ์ง ๋ชจ๋ฅด๊ฒ ์)
๋ฐ๋ผ์ ์์ฉ ํ๋ก๊ทธ๋จ์ ๋ช ์์ ์ผ๋ก ์์ฑ๋์ง ์์ ๊ฒ์ ์ฃผ์ ํ ์ ์๋ค.
๋ชจ๋ ๊ฒ์ด ๋ ๋๋ง๋๊ธฐ ์ ์ ๋ฌธ์์ด๋ก ๋ณํ๋๋ค.
์ด๋ฅผ ํตํด XSS(cross-site-scripting)๊ณต๊ฒฉ์ ๋ฐฉ์งํ ์ ์๋ค.
(์ ๋ ๋๋๊ธฐ ์ ์ ๋ฌธ์์ด๋ก ๋ณํ๋๋ฉด XSS ๋ง์ ์ ์๋์ง๋ ๋ชจ๋ฅด๊ฒ ์)
JSX Represents Objects
Babel compiles JSX down to React.createElement() calls.
These two examples are identical:
const element = ( <h1 className="greeting"> Hello, world! </h1> );
const element = React.createElement( 'h1', {className: 'greeting'}, 'Hello, world!' );
React.createElement() performs a few checks to help you write bug-free code but essentially it creates an object like this:
// Note: this structure is simplified const element = { type: 'h1', props: { className: 'greeting', children: 'Hello, world!' } };
These objects are called “React elements”. You can think of them as descriptions of what you want to see on the screen. React reads these objects and uses them to construct the DOM and keep it up to date.
We will explore rendering React elements to the DOM in the next section.
Tip:
We recommend using the “Babel” language definition for your editor of choice so that both ES6 and JSX code is properly highlighted.
'web > React&Redux' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ