๐ React
- React ์ค๋นํ๊ธฐ
* Node.js ์ค์น ๋ฆฌ๋
์ค, Mac
$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
$ nvm install --lts
์๋์ฐ๋ => https://nodejs.org/en/ => LTS ๋ฒ์ ์ค์น
```
* ์ ํ๋ก์ ํธ ๋ง๋ค์ด ๋ณด๊ธฐ
$ npx create-react-app ํ์ผ์ด๋ฆ
=> ํ์ผ์ด ์์ฑ์ด ๋ฉ๋๋ค. $ cd ํ์ผ์ด๋ฆ
$ npm start ํ๋ฉด ๋ค์ดํ์ ๋ฆฌ์กํธ๊ฐ ๋น๊ธ๋น๊ธ ํฉ๋๋ค.
## ๐ React - JSX ๊ท์น
1. ํญ์ ํ๊ทธ๋ก ์ด์์ผ๋ฉด ๋ซ๊ธฐ
2. { react } ์ด๋ ๊ฒ ์ค๊ดํธ๋ฅผ ์ฌ์ฉํ์ฌ์ ๋ฃ๊ณ ๊ฐ ์ฌ์ด์ ๊ณต๋ฐฑ์ฃผ๊ธฐ
APP.js ์์
import React from 'react';
import Example from './Example';
import "./App.css";
function App() {
const name = 'react';
const style = {
backgroundColor: 'black',
color: 'aqua',
fonstSize : 24,
padding: '1rem'
};
return (
<div>
<Example />
<div style={ style }>{ name }</div>
<div className="box"></div>
{ }
</div>
);
}
Exapmle.js
import React from 'react';
function Example() {
return <div>ํ์ด์</div>
}
export default Example;
๐ props๋ฅผ ํตํด ์ปดํฌ๋ํธ์ ๊ฐ ์ ๋ฌ
APP.js ์์
import React from 'react';
import Example from './Example';
function App() {
return (
<>
<Example name="react" color="red" />
<Example color="blue" />
</>
)
}
Exapmle.js
import React from 'react';
function Example(props) {
console.log(props);
return <div style={ {
color
} }>ํ์ด์ { props.name }</div>
}
** Example.js ๋น๊ตฌ์กฐ ํ ๋น์ผ๋ก ๋ฐ๊ฟ๋ณด๊ฒ ์ต๋๋ค. (function ๋ง ๊ฐ๋ตํ ๋ฐ๊พธ๊ฒ ์ต๋๋ค) **
function Example({ color, name }) {
return <div style = { {
color : color
} }>ํ์ด์ { name }</div>
Example.defaultProps = {
name : '์ด๋ฆ์์'
}
export default Example;
๐ ์์์์ฑ
APP.js ์์
import React from 'react';
import Example from './Example';
import Conatiner from './Container';
function App() {
return (
<Container>
<Example name="react" color="red" />
<Example color="blue" />
<Container/>
)
}
Example.js ์์
function Example({ color, name }) {
return <div style = { {
color : color
} }>ํ์ด์ { name }</div>
Example.defaultProps = {
name : '์ด๋ฆ์์'
}
export default Example;
์๋ก์ด Container.js ์์
import React from 'react';
function Container( { children }) {
const style = {
border: '3px solid black',
padding : 10
};
return <div style= { style } > { children }></div>
}