[KDT]FCFE-9週1日Reduxビュー(ReactContextが存在し、Reduxが必要な理由から)


Redux


なぜReact Contextが必要なのですか?

  • は、まず両方を選択するのではなく、両方とも一緒に使用することができます.
  • React Contextのみを使用する場合の潜在的な欠点

  • Complex Setup/Management
  • :大規模なプロジェクトを行うと、Context Providerが多すぎます.(複雑度が高い)
    return (
      <AuthContextProvider>
        <ThemeContextProvider>
          <UIInteractionContextProvider>
            <MultiStepFromContextProvider>
              <UserRegistration />
            </MultiStepFromContextProvider>
          </UIInteractionContextProvider>
        </ThemeContextProvider>
      </AuthContextProvider>
    )
  • Perfomance
  • :状態変動回数が多い場合は使いにくい.

    Reduxの構造



  • 1つのプロジェクトに1つのリポジトリ(中央データ・リポジトリ)のみがプロジェクト全体に使用されます.

  • 中央リポジトリは、特定のコンポーネント(設定済み)をサブスクリプションし、中央データ・リポジトリのステータスが変更されたサブスクリプション・コンポーネントに通知します.

  • storeでデータを直接操作しません.

  • アセンブリは減速機に動作を伝達します.reduceは、受信した操作を確認し、対応する操作を使用して中央リポジトリのデータを変更します.
  • Reducer Function

  • の純関数でなければなりません.関数
  • は同じ値を返します.
    const redux = require('redux');
    
    // state에 초기값 설정
    const couterReducer = (state = {counter:0}, action)=>{
      return {
        counter: state.counter +1
      };
    };
    
    const store = redux.createStore(counterReducer);
    
    const couterSubscriber =() =>{
      // 스토에 안에 최신 업데이트된 data를 가져올수 있는 함수 
      const latestState = store.getState();
      console.log(latestState);
    }
    
    // data를 가져간 함수를 구독하여 data가 최신화 되면 업데이트 해줌.
    store.subscribe(counterSubscriber);