Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

React + Reduxを基本からまとめてみた【5】【Storeとの接続】

Last updated at Posted at 2021-10-06

Fluxフロー図

![flux_flow.png]
(https://camo.qiitausercontent.com/cdd0824a85a48f51dacc8ec20fa5cf5ce6ec8093/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3434303539332f33313139643830622d383163642d633664382d636233312d6132383837386261313738312e706e67)

Storeとは

Storeはstateを保存する

  1. StoreとReducersを関連づける
  2. Redux(Store)とReactを接続する
  3. Storeの状態を変更する

Store | モジュールのimport

  1. reduxモジュールのimport
  2. Reducersのimport
store.js
//src/deducks/store/store.js
// 1.reduxモジュールのimport
import {
    createStore as reduxCreateStore,
    combineReducers
} from 'redux';

// 2.Reducersのimport
import { ProductsReducer } from '../products/reducers';
import { UsersReducer } from "../users/reducers";

Store | createStore関数の定義

1.reduxのcreateStoreメソッドをreturn
2.combineReducersでstateを生成

store.js
//src/deducks/store/store.js
export default function createStore() {
    // 1.reduxのcreateStoreメソッドをreturn
    return reduxCreateStore(
        // 2.combineReducersでstateを生成
        combineReducers({
            products: ProductsReducer,
            users: UsersReducer
        })
    );
}

combineReducersとは

1.分割したReducersをまとめる
2.stateのカテゴリ毎
3.オブジェクトをreturnする(stateのデータ構造)

sample.js
combineReducers({
  products: ProductsReducer,
  users: UsersReducer
})
//     ⬇︎
{
  products: {
    // productsのstate
  },
  users: {
    isSignedIn: false,
    uid: "",
    username: ""
  }
}

StoreとReactアプリの接続

src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import createStore from "./reducks/store/store";
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

export const store = createStore();

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
);
serviceWorker.unregister();

react-reduxのProviderとは

  1. propsにstoreを渡す
⇨ ラップしたコンポーネントにstoreの情報を渡す
  1. Reactコンポーネント内でreact-reduxのconnect関数を使えるようにする
⇨ reactとReduxを接続してstareを変更できるように

参考サイト

[日本一わかりやすいReact-Redux入門#6...Redux(Store)とReactを接続してstateを変更しよう]
(https://www.youtube.com/watch?v=t_-Tpc913B4)
[React- Redux 入門]
(https://qiita.com/shtwangy/items/4f9993ade64c3c6858d3)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?