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 1 year has passed since last update.

Redux toolkitのthunk関数の型定義について

Last updated at Posted at 2024-02-13

背景

認証の非同期処理をthunkを使って、サインインが完了したらreduxの値を更新したい。
redux toolkitのcreateAsyncThunkを使う。
createAsyncThunkでrejectWithValueを使った時のpayloadの型がanyになる。

rejectWithValueを使った時のpayloadの型がanyになる

スクリーンショット 2024-02-13 19.24.28.png

react.js
export const signInThunk = createAsyncThunk(
  'auth/signIn',
  async (signInArgs: SignInArgs, { rejectWithValue }) => {
    try {
      const result = await signInFirebaseWithEmailAndPassword({ ...signInArgs });
      return { loading: false, error: [], identity: result.identity, statusCode:result.statusCode };
    } catch () {
      return rejectWithValue({
        statusCode: result.statusCode,
      });
    }
  },
);
export const authSlice = createSlice({
  name: 'auth',
  initialState,
  reducers: {},
  extraReducers: (builder) => {
    // auth/signIn
    builder.addCase(signInWithEmailAndPasswordThunk.fulfilled, (_, action) => ({
      ...action.payload,
    }));
    builder.addCase(signInWithEmailAndPasswordThunk.pending, (state) => {
      state.loading = true;
    });
    builder.addCase(signInWithEmailAndPasswordThunk.rejected, (state, action) => {
      state.loading = false;
      state.error = ['Auth error'];
      state.statusCode = action.payload?.statusCode;
    });
  },
});

解決策

createAsyncThunk<T>のTの型を定義する。これだけだけど、型の順番に注意が必要。以下の順番になっている。

  • 成功した時の戻り値の型
  • 引数の型
  • rejectWithValueで渡す型
react.js
export const signInThunk = createAsyncThunk<
  AuthState, // 成功した時の戻り値の型
  SignInArgs, // 引数の型
  { rejectValue: Pick<AuthState, 'statusCode'> } // rejectWithValueで渡す型
>(
  'auth/signIn',
  async (signInArgs: SignInArgs, { rejectWithValue }) => {
    try {
      const result = await signInFirebaseWithEmailAndPassword({ ...signInArgs });
      return { loading: false, error: [], identity: result.identity, statusCode:200 };
    } catch () {
      return rejectWithValue({
        statusCode: result.statusCode,
      });
    }
  },
);

終わり

redux toolkitは知らないことがまだまだいっぱい。.

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
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?