Help us understand the problem. What is going on with this article?

Redux-hooks【短編】

概要

Reactで開発するにあたってHooksちゅう新しい機能を使うので、勉強したことをメモ的に投稿していこうと思います。

公式ドキュメント

React Redux(Hooks)

トピックス
・useSelector
・useDispatch

useSelector

selecter関数をしてStoreのstateからデータを取得します。
useSelectorはStoreを監視し、アクションがディスパッチされるたびにselecter関数を実行します。

例:useSelector

useSelector
 const selector = state => state.name;
 const name = useSeletor(selector);

useDispatch

Storeからdispatch関数を返します。
dispatch関数を使用してアクションをディスパッチできます。

例:useDispatch

useDispatch
 const dispatch = useDispatch();
 dispatch({ type: "LOGIN", 'くいだおれ太郎' });

useSelector & useDispatch

実際に使ってみたサンプル

example.jsx
import React from "react";
import { useDispatch, useSelector } from "react-redux";

const Example = () => {
    /* useSelector */
    const name = useSeletor(state => state.name);
    /* useDispatch */
    const dispatch = useDispatch();

    return(
        <>
            <h1>NAME: {name}</h1>
            <input
                type="text"
                onChange={event =>
                    dispatch({ type: "NAME", event.target.value })} />
        </>
    );
}

ざっくりまとめ

Action, Reducerの設定は今まで通り必要ですが、connect関数を使用した、めんどくさい設定がいらんようなって楽チンになりました。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした