Skip to content

Instantly share code, notes, and snippets.

@yuki-yano
Created November 30, 2022 07:51
Show Gist options
  • Select an option

  • Save yuki-yano/da2372e6a5bf36ed1b9533ee7f0822c7 to your computer and use it in GitHub Desktop.

Select an option

Save yuki-yano/da2372e6a5bf36ed1b9533ee7f0822c7 to your computer and use it in GitHub Desktop.
deno redux-toolkit counter
import { default as redux } from "npm:@reduxjs/[email protected]";
import type { PayloadAction } from "npm:@reduxjs/[email protected]";
import { default as logger } from "npm:[email protected]";
import { default as Logger } from "npm:@types/[email protected]";
const { configureStore, createSlice } = redux;
const { createLogger } = logger as typeof Logger;
type CounterState = {
count: number;
};
const initialState: CounterState = {
count: 0,
};
const counterSlice = createSlice({
name: "counter",
initialState,
reducers: {
increment: (state) => {
state.count += 1;
},
decrement: (state) => {
state.count -= 1;
},
incrementByAmount: (state, action: PayloadAction<{ num: number }>) => {
state.count += action.payload.num;
},
},
});
const store = configureStore({
reducer: { counter: counterSlice.reducer },
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware().concat(createLogger()),
});
const { dispatch } = store;
const counterSelector = (): CounterState["count"] =>
store.getState().counter.count;
dispatch(counterSlice.actions.increment());
console.log(counterSelector());
dispatch(counterSlice.actions.incrementByAmount({ num: 10 }));
console.log(counterSelector());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment