๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

200314

ยท ์•ฝ 5๋ถ„

React#

๋ฒจ๋กœํผํŠธ์™€ ํ•จ๊ป˜ํ•˜๋Š” ๋ชจ๋˜ ๋ฆฌ์•กํŠธ


React Component ๋งŒ๋“ค๊ธฐ#

Hello.js

import React from 'react';
function Hello() {    return <div>Hello World!</div>;}
export default Hello;

๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค ๋•

import React from 'react';

๋ฅผ ๋„ฃ์–ด์ค˜์•ผํ•œ๋‹ค.

๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ๋Š” XML ํ˜•์‹์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด๋ฅผ JSX ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

export default Hello;

์ด ์ฝ”๋“œ๋Š” Hello ๋ผ๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋‚ด๋ณด๋‚ด๊ฒ ๋‹ค๋Š” ์˜๋ฏธ.

App.js

import React from 'react';import Hello from './Hello';
function App() {    return (        <div>            <Hello />        </div>    );}
export default App;

์‹คํ–‰์‹œ์ผœ๋ณด๋ฉด, ( yarn start or npm start )

2020-03-14-200314-image-0

index.js

import React from 'react';import ReactDOM from 'react-dom';import './index.css';import App from './App';import * as serviceWorker from './serviceWorker';
ReactDOM.render(<App />, document.getElementById('root'));
// If you want your app to work offline and load faster, you can change// unregister() to register() below. Note this comes with some pitfalls.// Learn more about service workers: https://bit.ly/CRA-PWAserviceWorker.unregister();

์—ฌ๊ธฐ์„œ ReactDOM.render ๋ฅผ ๋ณด๋ฉด, id ๊ฐ€ root ์ธ DOM ๋‚ด๋ถ€์— ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ( <App /> )๋ฅผ ๋ Œ๋”๋งํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

์ด DOM์€ public/index.html ๋‚ด๋ถ€์— ์žˆ๋‹ค.

<div id="root"></div>

JSX#

JavaScript + XML, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ํ™•์žฅ ๋ฌธ๋ฒ•. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋‚ด๋ถ€์— ๋งˆํฌ์—… ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ์ค„ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์คŒ. HTML ๊ฐ™์ด ์ƒ๊ฒผ์ง€๋งŒ ์‹ค์ œ๋ก  JS ์ด๋‹ค.

return <div>์•ˆ๋…•ํ•˜์„ธ์š”</div>;

Babel ์€ XML ํ˜•ํƒœ์˜ ์ฝ”๋“œ๋ฅผ JS๋กœ ๋ณ€ํ™˜ํ•ด์ค€๋‹ค.

๊ทœ์น™#

  • ํƒœ๊ทธ๋ฅผ ์—ด์—ˆ์œผ๋ฉด ๋‹ซ์•„์•ผ ํ•จ. ( input , br ํฌํ•จ ๋ชจ๋“  ํƒœ๊ทธ. ํƒœ๊ทธ ์‚ฌ์ด์— ๋‚ด์šฉ์ด์—†์„ ๋•, <input/> ๋กœ ์…€ํ”„ํด๋กœ์ง• ๊ฐ€๋Šฅ)

  • ๋‘ ๊ฐœ ์ด์ƒ์˜ ํƒœ๊ทธ๋Š” ํ•˜๋‚˜์˜ ํƒœ๊ทธ๋กœ ๊ฐ์‹ธ์ ธ ์žˆ์–ด์•ผ ํ•œ๋‹ค. <> </> Fragment๋กœ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๋ถˆํ•„์š”ํ•œ ํƒœ๊ทธ ์—†์ด ๋‘ ๊ฐœ ์ด์ƒ์˜ ํƒœ๊ทธ ๊ฐ€๋Šฅ.

  • JSX ๋‚ด๋ถ€์— ๋ณ€์ˆ˜๋Š” {} ๋กœ ๊ฐ์‹ธ์„œ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ์Œ.


style ๊ณผ className#

style ๊ณผ CSS class ์„ค์ • ๋ฐฉ๋ฒ•์ด HTML๊ณผ ๋‹ค๋ฅด๋‹ค.

  • ์ธ๋ผ์ธ ์Šคํƒ€์ผ์€ ๊ฐ์ฒด์ฒ˜๋Ÿผ ์ž‘์„ฑ.

  • background-color ๊ฐ™์ด - ๋กœ ๊ตฌ๋ถ„๋œ ์†์„ฑ ์ด๋ฆ„์„ ๊ฐ€์ง„ ์†์„ฑ๋“ค์€ camelCase ํ˜•ํƒœ๋กœ ( backgroundColor )

  • CSS class ์„ค์ •์‹œ์—” class="" ๊ฐ€ ์•„๋‹Œ className="" ์œผ๋กœ.

App.js

import React from 'react';import Hello from './Hello';import './App.css';
function App() {    const name = 'react';    const style = {        backgroundColor: 'black',        color: 'aqua',        fontSize: 24, // ๊ธฐ๋ณธ ๋‹จ์œ„ px        padding: '1rem', // ๋‹ค๋ฅธ ๋‹จ์œ„ ์‚ฌ์šฉ ์‹œ ๋ฌธ์ž์—ด๋กœ ์„ค์ •    };
    return (        <>            <Hello />            <div style={style}>{name}</div>            <div className="gray-box"></div>        </>    );}
export default App;

App.css

.gray-box {    background: gray;    width: 64px;    height: 64px;}

์ฃผ์„#

{/* JSX ๋‚ด๋ถ€ ์ฃผ์„ ์ž‘์„ฑ๋ฒ• */}

์—ด๋ฆฌ๋Š” ํƒœ๊ทธ ๋‚ด๋ถ€์—์„œ๋Š” // ์ด๋Ÿฐ ํ˜•ํƒœ์˜ ์ฃผ์„ ์ž‘์„ฑ๋„ ๊ฐ€๋Šฅ


props ๋ฅผ ํ†ตํ•ด ์ปดํฌ๋„ŒํŠธ์— ๊ฐ’ ์ „๋‹ฌ#

App ์ปดํฌ๋„ŒํŠธ์—์„œ Hello ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๊ฐ’์„ ์ „๋‹ฌํ•˜๊ณ  ์‹ถ์„ ๋•Œ

App.js

import React from 'react';import Hello from './Hello';
function App() {    return <Hello name="react" />;}
export default App;

Hello.js

import React from 'react';
function Hello(props) {    return <div>์•ˆ๋…•ํ•˜์„ธ์š” {props.name}</div>;}
export default Hello;

props ๋ผ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์กฐํšŒ, ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ์ „๋‹ฌ๋˜๋ฏ€๋กœ props.name ์œผ๋กœ ์กฐํšŒ ๊ฐ€๋Šฅ


๋น„๊ตฌ์กฐํ™” ํ• ๋‹น#

App.js

import React from 'react';import Hello from './Hello';
function App() {    return <Hello name="react" color="red" />;}
export default App;

name , color ๋‘ ๊ฐ€์ง€ ๊ฐ’์„ ์ „๋‹ฌ.

import React from 'react';
function Hello(props) {    return <div style={{color: props.color}}>์•ˆ๋…•ํ•˜์„ธ์š” {props.name}</div>;}
export default Hello;

color: props.color ๋Š” ๊ฐ์ฒด์ด๋ฏ€๋กœ {} ๋กœ ํ•œ ๋ฒˆ ๊ฐ์‹ธ๊ณ , ์ด ๊ฐ์ฒด๊ฐ€ JS ๊ฐ’์ด๋ฏ€๋กœ {} ๋กœ ํ•œ๋ฒˆ ๋” ๊ฐ์‹ผ๋‹ค.


๋‘ ๊ฐ€์ง€ ๊ฐ’์„ ์ „๋‹ฌํ•  ๋•Œ ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น์œผ๋กœ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ž‘์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

import React from 'react';
function Hello({color, name}) {    return <div style={{color}}>์•ˆ๋…•ํ•˜์„ธ์š” {name}</div>;    // color: color -> color ๋กœ ์ž‘์„ฑ ๊ฐ€๋Šฅ}
export default Hello;