Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

how to call an action from another action in redux

const counter = createSlice({
  name: 'counter',
  initialState: 0 as number,
  reducers: {
    increment: (state) => state + 1,
    decrement: (state) => state - 1,
    multiply: {
      reducer: (state, action: PayloadAction<number>) => state * action.payload,
      prepare: (value?: number) => ({ payload: value || 2 }), // fallback if the payload is a falsy value
    },
  },
  // "builder callback API", recommended for TypeScript users
  extraReducers: (builder) => {
    builder.addCase(incrementBy, (state, action) => {
      return state + action.payload
    })
    builder.addCase(decrementBy, (state, action) => {
      return state - action.payload
    })
  },
})

const user = createSlice({
  name: 'user',
  initialState: { name: '', age: 20 },
  reducers: {
    setUserName: (state, action) => {
      state.name = action.payload // mutate the state all you want with immer
    },
  },
  // "map object API"
  extraReducers: {
    // @ts-expect-error in TypeScript, this would need to be [counter.actions.increment.type]
    [counter.actions.increment]: (
      state,
      action /* action will be inferred as "any", as the map notation does not contain type information */
    ) => {
      state.age += 1
    },
  },
})
Comment

PREVIOUS NEXT
Code Example
Typescript :: matplotlib eats all memory when saving fig 
Typescript :: 4. In order to have proper integration of the pulse current it is desired that 
Typescript :: How to check that tuple contains unique elements 
Typescript :: which of the following object types below cannot be replicated 
Typescript :: calculate north south east west using magnetic sensor 
Typescript :: why do we use #Email in angular with ngmodel 
Typescript :: TypeError: agent_go() takes 0 positional arguments but 1 was given 
Typescript :: extracts lists from list python 
Typescript :: typescript import variable from another file 
Typescript :: fiber absorption loss measurement 
Typescript :: github:Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.15 in android 
Typescript :: INTENT 
Typescript :: typescript mocha Cannot use import statement outside a module 
Typescript :: angular input change event datatype typescript 
Typescript :: tiqets endpoints 
Typescript :: windows 10 iso 
Cpp :: git branch in my bash prompt 
Cpp :: excel vba delete worksheet if exists 
Cpp :: c++ generate random char 
Cpp :: c++ find minimum value in vector 
Cpp :: c++ read console input 
Cpp :: how to cehck if list has element c++ 
Cpp :: how to sort in descending order c++ 
Cpp :: qt qlcdnumber change value 
Cpp :: what are specialized classes c++ 
Cpp :: what are various sections of process 
Cpp :: did greeks write c++ codes? 
Cpp :: call of overloaded ‘swap(int&, int&)’ is ambiguous 
Cpp :: how can I replace a pattern from string in c++ 
Cpp :: PI IN C++ WITH CMATH 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =