ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 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
    
Designed by Tistory.