์ปค์Šคํ…€ํ›…์œผ๋กœ ํˆฌ๋‘๋ฆฌ์ŠคํŠธ input ๋ฆฌํŒฉํ† ๋งํ•˜๊ธฐ
728x90

Today, What I learned?

์ปค์Šคํ…€ ํ›…์— ์ต์ˆ™ํ•ด์ ธ ๋ณด๊ธฐ ์œ„ํ•ด ํˆฌ๋‘๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฆฌํŒฉํ„ฐ๋ง ํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.
๊ธฐ์กด์˜ ์ฝ”๋“œ ์ค‘์—์„œ ํˆฌ๋‘๋ฆฌ์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„์„ ์ปค์Šคํ…€ํ›…์œผ๋กœ ๋งŒ๋“ค์–ด ๋ณด์•˜๋‹ค.

state์™€ dispatch, submitHandler ๊นŒ์ง€ ๋ชจ๋‘ ๊ฐ€์ ธ์™”๋‹ค.

 

useInput

import { useState } from 'react';
import { useDispatch } from 'react-redux';
import { nanoid } from '@reduxjs/toolkit';
import { __addTodos } from '../redux/modules/todoSlice';

const useInput = () => {
  const [title, setTitle] = useState('');
  const [content, setContent] = useState('');

  const dispatch = useDispatch();

  ...
    const newTodo = {
      id: nanoid(),
      title,
      content,
      isDone: false,
    };

    dispatch(__addTodos(newTodo));

    setTitle('');
    setContent('');
    return true;
  };

  return { title, content, changeTitleHandler, changeContentHandler, submitHandler };
};

export default useInput;

 

 

๋ฆฌํŒฉํ† ๋ง ํ›„

์ปค์Šคํ…€ ํ›…์„ ์ ์šฉํ•˜๊ณ  ๋‚˜๋‹ˆ๊นŒ ์ปดํฌ๋„ŒํŠธ์— UI๋งŒ ๋‚จ์•„์„œ ํ•œ๊ฒฐ ๋ณด๊ธฐ ํŽธํ•˜๊ณ , ๊ด€๋ จ๋œ ๋กœ์ง์„ ํ•œ๋ˆˆ์— ์•Œ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์–ด ํŽธํ•œ ๊ฒƒ ๊ฐ™๋‹ค.

728x90