mirror of
https://github.com/TheGreyDiamond/Enlight.git
synced 2026-03-31 23:00:24 +02:00
init
This commit is contained in:
38
enlightApp/app/features/counter/counterSlice.ts
Normal file
38
enlightApp/app/features/counter/counterSlice.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { createSlice } from '@reduxjs/toolkit';
|
||||
// eslint-disable-next-line import/no-cycle
|
||||
import { AppThunk, RootState } from '../../store';
|
||||
|
||||
const counterSlice = createSlice({
|
||||
name: 'counter',
|
||||
initialState: { value: 0 },
|
||||
reducers: {
|
||||
increment: (state) => {
|
||||
state.value += 1;
|
||||
},
|
||||
decrement: (state) => {
|
||||
state.value -= 1;
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const { increment, decrement } = counterSlice.actions;
|
||||
|
||||
export const incrementIfOdd = (): AppThunk => {
|
||||
return (dispatch, getState) => {
|
||||
const state = getState();
|
||||
if (state.counter.value % 2 === 0) {
|
||||
return;
|
||||
}
|
||||
dispatch(increment());
|
||||
};
|
||||
};
|
||||
|
||||
export const incrementAsync = (delay = 1000): AppThunk => (dispatch) => {
|
||||
setTimeout(() => {
|
||||
dispatch(increment());
|
||||
}, delay);
|
||||
};
|
||||
|
||||
export default counterSlice.reducer;
|
||||
|
||||
export const selectCount = (state: RootState) => state.counter.value;
|
||||
Reference in New Issue
Block a user