[React], useSelector, useDispatch로 state에 접근하기

SeungMai(junior)·2021년 9월 29일
0

react-redux

yarn add react-redux 설치

  • state를 조회하기 위한 useSelector를 사용할 수 있다.
  • action을 빌생시키기 위한 useDispatch를 사용할 수 있다.

useSelector

  • connect함수를 이용하지 않고 리덕스의 state를 조회할 수 있다.
import { useSelector, useDispatch } from "react-redux";

const dictionary_data = useSelector((state) => state.dictionary.list);

console.log(dictionary_data);

useDispatch

  • 생성한 action을 useDispatch를 통해 발생시킬 수 있다.
  • 만들어둔 액션생성 함수를 import한다.
import { useDispatch } from "react-redux";
import { addDictionaryFB } from "./redux/modules/dictionary";

const dispatch = useDispatch();

  const dictionary_index = () => {
    const card = {
      word: word.current.value,
      account: account.current.value,
      example: example.current.value,
    };
    dispatch(addDictionaryFB(card));
  };
profile
👍🏻 SeungMai (매 순간 기록을!)🧑🏻‍💻 | WIL만 올리고 있습니다 | 기술블로그는 아래 🏠 의 링크를 이용해주세요.

0개의 댓글