0
1

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エラー備忘録2 : Error: Reducer "products" returned undefined during initialization.

0
Last updated at Posted at 2021-01-27

概要

React アプリを開発する上で発生したエラーの原因と対策を忘れないためのメモです。

状況

某商品紹介用の Web アプリの商品追加画面制作中に発生。

エラー

> Error: Reducer "products" returned undefined during initialization. If the state passed to the reducer is undefined, you must explicitly return the initial state. The initial state may not be undefined. If you don't want to set a value for this reducer, you can use null instead of undefined.

コード

export const ProductsReducer = (state = initialState.products, action) => {
  switch (action.type) {
  }
};

原因

reducer の switch 文の中に default のケースを設定していなかったこと

対策

switch 文に default のケースを追加することで解決

export const ProductsReducer = (state = initialState.products, action) => {
  switch (action.type) {
    default:
      return state;
  }
};
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?