3
4

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.

Extra Reducerを理解してなかった話

Posted at

Extra ReducerをcreateAsyncActionの付き添いレベルで想定していたが、domainを跨いでactionによるStoreの更新を行える。

ログアウトや、cacheのアップデート(apiのresponseがnestedで他のデータに影響する場合(user.oragnizationとか))の場合は影響範囲をdomain単位で記載できる。

変更による改修コストは確かにあがりそうだけど、importから終える+createAsyncThunkで大量のdispatchを呼ばなくてすむので、コードの集約生が上がりそう。

ちゃんと解説を読むべきだった。。。
createSlice

store-sample.ts
const Logout = createAction('[CORE] LOGOUT')

const userSlice = createSlice({
    name: "user",
    reducers: {
       clear: () => initialState
    },
    extraReducers: builder => {
        builder.addCase(Logout, (state, action) => {
              console.log("call because of logout") // 呼ばれる
              userSlice.caseReducer.clear(state, action)
        }).addDeXXXX
    }
})

const companySlice = createSlice({
    name: "company",
    reducers: {
       clear: () => initialState
    },
    extraReducers: builder => {
        builder.addCase(Logout, (state, action) => {
              console.log("call because of logout") // 呼ばれる
              userSlice.caseReducer.clear(state, action)
        }).addDeXXXX
    }
})
contaienr.tsx
const App: FC<Props> = props => {
    return (
        <button onClick={() => {useDispatch(Logout())}} >logout</button>
    )
}
3
4
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
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?