-
REACT ์์ - โ๏ธReact 2023. 7. 29. 15:51
๊ฐ์ ๋งํฌ : https://www.youtube.com/watch?v=6GECT2Jrr_g
react ํ๋ก์ ํธ ์์ฑ
- npm init react-app ํ๋ก์ ํธ์ด๋ฆ
- package.json์ ๋ณด๋ฉด ๊ฐ ๋ชจ๋๋ค ์ค์น ๋ด์ฉ๋ค์ด ๋ค์ด์๋ค.
- npm start ํ๋ฉด index.js ํ์ผ์ด ์คํ๋๋ค.
- JSX - JavaScriptXml html์ ์๋ฐ์คํฌ๋ฆฝํธ ์์์ ์ธ ์ ์๊ฒ ํจ.
- index.js ์ App.js import >
- root id ์ฐพ์์ create Root๋ก ๋ง๋ค์ด์ฃผ๊ณ ๊ทธ์์ App์ ๋๋ ํจ.
- reportWebVitals() : ์น ํผํฌ๋จผ์ค ์ธก์ ์ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
App.js ์๋ฐ์คํฌ๋ฆฝํธ์์ HTML ์ฌ์ฉ ⇒ JSX
์ปดํฌ๋ํธ ๋ฐฉ์
- ํจ์ํ ์ปดํฌ๋ํธ ๋ฐฉ์ (์์ ๋ง์ด ์ฐ์ด๋ ๋ฐฉ์)
(ํจ์ํ ์ปดํฌ๋ํธ ์ ๋ง์ ๋ง๊ฒ ํ์ดํ ํจ์๋ก ๋ณ๊ฒฝํด์ ์ฌ์ฉ)
- ํด๋์ค ์ปดํฌ๋ํธ ๋ฐฉ์
์ฑ๊ธํ์ด์ง ์ ํ๋ฆฌ์ผ์ด์ : ๋ฉ๋ด๋ฅผ ํด๋ฆญํ ๋ ๋ง๋ค ์๋ฒ์์ ํ์ด์ง๊ฐ ์ปดํ์ผ๋์ ํ์ด์ง๊ฐ ๋ ์์ค๋ ๊ฐ๋ ์ด ์๋, ์ค์ ๋ก๋ index.html ํ์ด์ง๊ฐ ํ๋๋ฐ ๋ฉ๋ด๋ฅผ ํด๋ฆญํ ๋ ๋ง๋ค ํ์ํ ๋ถ๋ถ๋ง ๋ฐ๊ฟ ๋๊ฐ๋ ๋ฐฉ๋ฒ์ด๋ค.
react ๊ธฐ์ด ๋ฐฐ์ฐ๊ธฐ
- ๋ฉ๋ด๋ฅผ ํด๋ฆญ ํ ๋๋ง๋ค ์ํ๋ ์ปดํฌ๋ํธ๋ฅผ ๋งคํ์ํค๊ธฐ๋ ๊ฒ์ ๋ผ์ฐํ ์ฒ๋ฆฌ๋ฅผ ํ๋ค๊ณ ํ๋ค.
- ๋ธ๋ผ์ฐ์ url์ด ๋ฐ๋๋๋ง๋ค ๋๋๋ง ๋๋ค.
- router ๋ฅผ ์ํ ๋ชจ๋ ์ค์น
- npm install react-router-dom@6
- ์ค์น ์์น ์ฃผ์!
router ์ฌ์ฉ ๋ฐฉ๋ฒ
index.js
import React from 'react'; import ReactDOM from 'react-dom/client'; import {BrowserRouter} from 'react-router-dom'; import './index.css'; import App from './App'; import reportWebVitals from './reportWebVitals'; const root = ReactDOM.createRoot(document.getElementById('root')); root.render( ); // If you want to start measuring performance in your app, pass a function // to log results (for example: reportWebVitals(console.log)) // or send to an analytics endpoint. Learn more: <https://bit.ly/CRA-vitals> reportWebVitals();
App.js
import React from 'react'; import { Routes, Route, Link } from "react-router-dom"; import Home from "./pages/Home"; import About from "./pages/About"; import Counter from "./pages/Counter"; import Input from "./pages/Input"; import Input2 from "./pages/Input2"; import List from "./pages/List"; function App() { return ( <div className="App"> <nav> <Link to='/'>HOME</Link> || <Link to='/about'>ABOUT</Link> || <Link to='/counter'>Counter</Link> || <Link to='/input'>Input</Link> || <Link to='/input2'>Input2</Link> || <Link to='/list'>List</Link> </nav> <Routes> <Route path='/' element={<Home></Home>}></Route> <Route path='/about' element={<About></About>}></Route> <Route path='/counter' element={<Counter></Counter>}></Route> <Route path='/input' element={<Input></Input>}></Route> <Route path='/input2' element={<Input2></Input2>}></Route> <Route path='/list' element={<List></List>}></Route> </Routes> </div> ); } export default App;
state์ ์ ์ฅํ ๋ฐ์ดํฐ ๋ณํ ๊ฐ์ง ๋๋ฉด ๊ณ์ฐ ํ ํ๋ฉด์ ๋๋๋ง
import React, {useState} from "react"; const Counter = () => { const [num, setNumber] = useState(0); const increase = () => { setNumber(num+1); }; const decrease = () => { setNumber(num-1); }; return ( <div> <button onClick={increase}> +1 </button> <button onClick={decrease}> -1 </button> <p>{num}</p> </div> ) } export default Counter;
input์ผ๋ก ๋ฐ์ดํฐ ๋ฐ๊ณ ํ๋ฉด์ ๋๋๋ง & ๋ค์ค์ผ๋ก ๋ฐ์ ๋ฐ์ดํฐ ํ๋ฉด์ ๋๋๋ง
[input]
import React, {useState} from "react"; const Input = () => { const [txtValue, setTextValue] = useState(""); const onChange = (e) => { setTextValue(e.target.value); }; return ( <div> <input type="text" value={txtValue} onChange={onChange}/> <p>{txtValue}</p> </div> ) } export default Input;
[input2]
import React, {useState} from "react"; const Input2 = () => { const [inputs, setInputs] = useState({ name: "", email: "", tel: "", }); const {name, email, tel} = inputs; const onChange = (e) => { const value = e.target.value; const id = e.target.id; setInputs({ ...inputs, [id]: value, }); }; return ( <div> <label>์ด๋ฆ</label> <input type="text" id="name" value={name} onChange={onChange}/> <label>์ด๋ฉ์ผ</label> <input type="email" id="email" value={email} onChange={onChange}/> <label>์ ํ๋ฒํธ</label> <input type="tel" id="tel" value={tel} onChange={onChange}/> <p>{name}</p> <p>{email}</p> <p>{tel}</p> </div> ) } export default Input2;
๋ฐฐ์ด๋ก ๋ฐ์ ๋ฐ์ดํฐ map์ ์ด์ฉํ ๋ฐ๋ณต๋ฌธ ๋๋๋ง
// ๋ฐฐ์ด๋ก ๋ค์ด์ค๋ ๋ฐ์ดํฐ ๋๋๋ง ๋ฐฉ๋ฒ import React from "react"; const UserList = () => { const users = [ {email: "you@gmail.com", name:"์ ์ฌ์"}, {email: "song@gmail.com", name:"์ก์งํจ"}, {email: "ha@gmail.com", name:"ํํ"}, {email: "kim@gmail.com", name:"๊น์ข ๊ตญ"}, {email: "lee@gmail.com", name:"์ด๊ด์"}, ] const User = ({userData}) => { return ( <tr> <td>{userData.email}</td> <td>{userData.name}</td> </tr> ) } return ( <div> <table> <thead> <tr> <th>์ด๋ฉ์ผ</th> <th>์ด๋ฆ</th> </tr> </thead> <tbody> {users.map((user) => ( <User userData={user}/> ))} </tbody> </table> </div> ) } export default UserList