React :: ๋ฐฐ์—ด

s_omiยท2021๋…„ 12์›” 27์ผ
1

React (๋ฆฌ์•กํŠธ)

๋ชฉ๋ก ๋ณด๊ธฐ
1/6
post-thumbnail

๐Ÿ“ ๋ฐฐ์—ด์— ํ•ญ๋ชฉ ์ถ”๊ฐ€

// CreateUser.js

import React from 'react';

function CreateUser({ username, email, onChange, onCreate }) {
  // ์ƒํƒœ๊ด€๋ฆฌ๋ฅผ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์ธ App์—์„œ ํ•˜๋ฉฐ, input =์˜ ๊ฐ’ ๋ฐ ์ด๋ฒคํŠธ๋กœ ๋“ฑ๋กํ•  ํ•จ์ˆ˜๋“ค์„ props๋กœ ๋„˜๊ฒจ๋ฐ›์•„ ์‚ฌ์šฉ
  return (
    <div>
      <input
        name="username"
        placeholder="๊ณ„์ •๋ช…"
        onChange={onChange}
        value={username}
      />
      <input
        name="email"
        placeholder="์ด๋ฉ”์ผ"
        onChange={onChange}
        value={email}
      />
      <button onClick={onCreate}>๋“ฑ๋ก</button>
    </div>
  );
}

export default CreateUser;

// App.js
// CreateUser ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ํ•„์š”ํ•œ props ๋ฅผ App ์—์„œ ์ค€๋น„

import React, { useRef, useState } from 'react';
import UserList from './UserList';
import CreateUser from './CreateUser';

function App() {
  const [inputs, setInputs] = useState({
    username: '',
    email: ''
  });
  const { username, email } = inputs;
  const onChange = e => {
    const { name, value } = e.target;
    setInputs({
      ...inputs,
      [name]: value
    });
  };
  const users = [
    {
      id: 1,
      username: 'velopert',
      email: 'public.velopert@gmail.com'
    },
    {
      id: 2,
      username: 'tester',
      email: 'tester@example.com'
    },
    {
      id: 3,
      username: 'liz',
      email: 'liz@example.com'
    }
  ];

  const nextId = useRef(4);
  const onCreate = () => {
    // ๋‚˜์ค‘์— ๊ตฌํ˜„ ํ•  ๋ฐฐ์—ด์— ํ•ญ๋ชฉ ์ถ”๊ฐ€

    setInputs({
      username: '',
      email: ''
    });
    nextId.current += 1;
  };
  return (
    <>
      <CreateUser
        username={username}
        email={email}
        onChange={onChange}
        onCreate={onCreate}
      />
      <UserList users={users} />
    </>
  );
}

export default App;

๐Ÿ’ก users๋„ useState๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ปดํฌ๋„ŒํŠธ์˜ ์ƒํƒœ๋กœ์„œ ๊ด€๋ฆฌ

// App.js

  ... 
  
  const [users, setUsers] = useState([
    {
      id: 1,
      username: 'velopert',
      email: 'public.velopert@gmail.com'
    },
    {
      id: 2,
      username: 'tester',
      email: 'tester@example.com'
    },
    {
      id: 3,
      username: 'liz',
      email: 'liz@example.com'
    }
  ]);
  
  ...

๐Ÿ’ก ๋ถˆ๋ณ€์„ฑ์„ ์ง€ํ‚ค๋ฉด์„œ ๋ฐฐ์—ด์— ์ƒˆ ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•

โš  ๋ฐฐ์—ด์˜ push, splice, sort ๋“ฑ์˜ ํ•จ์ˆ˜ ์‚ฌ์šฉ X, ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๋ฉด ๊ธฐ์กด์˜ ๋ฐฐ์—ด์„ ํ•œ๋ฒˆ ๋ณต์‚ฌํ•œ ํ›„ ์‚ฌ์šฉ

๐Ÿ’ฌ spread ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ

// App.js

  ...

  const nextId = useRef(4);
  const onCreate = () => {
    const user = {
      id: nextId.current,
      username,
      email
    };
    setUsers([...users, user]);  // spread ์—ฐ์‚ฐ์ž

    setInputs({
      username: '',
      email: ''
    });
    nextId.current += 1;
  };
  
  ...

๐Ÿ’ฌ concat ํ•จ์ˆ˜ ์‚ฌ์šฉ

concat ํ•จ์ˆ˜ : ๊ธฐ์กด์˜ ๋ฐฐ์—ด์„ ์ˆ˜์ •ํ•˜์ง€ ์•Š๊ณ , ์ƒˆ๋กœ์šด ์›์†Œ๊ฐ€ ์ถ”๊ฐ€๋œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ƒ์„ฑ
// App.js

  ...

  const nextId = useRef(4);
  const onCreate = () => {
    const user = {
      id: nextId.current,
      username,
      email
    };
    setUsers(users.concat(user));  // concat ํ•จ์ˆ˜

    setInputs({
      username: '',
      email: ''
    });
    nextId.current += 1;
  };
  
  ...
profile
๊ณต๋ถ€ํ•œ ๊ฑฐ ์˜ฌ๋ ค์š” :)

0๊ฐœ์˜ ๋Œ“๊ธ€