Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

how to call an action from another action slice 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 :: typescript split/partition array by condition 
Typescript :: all default datasets in seaborn 
Typescript :: get database num hits django 
Typescript :: typescript onchane event 
Typescript :: requestRandomness 3 arguments given but expected 2 
Typescript :: subplots in for loop python (no dynamic) 
Typescript :: What types of Collections/Data structures you have used 
Typescript :: angular TS2377 
Typescript :: set in typescript 
Typescript :: dto typescript 
Typescript :: Which Protect Presentation option protects a presentation from accidental changes: 
Typescript :: ts Command pattern 
Typescript :: typescript generic object not array 
Typescript :: They Take Their Medication Then The Device Owner Lets Them Press The Button | The Problem We Are Solving Is Kids Not Taking Their Medication Which Turns To Great Health Benefits In The Young Generation 
Typescript :: how to get all posible subb lists in python 
Typescript :: teken aja 
Typescript :: macro fiji bio-formats import options 
Typescript :: print in a tsv file all names of files in a directory linux 
Typescript :: Do you use data structures in your current automation project 
Typescript :: The softness of a spot lights edge is controlled by penumbra angle, value gives perfect hard edge: 
Typescript :: products = product.object.all() python 
Typescript :: why table columns are messing in one another in angular 
Typescript :: why my res.data returns array of objects ? (AngularJs) 
Typescript :: Restrict users to see only his own contacts odoo 
Typescript :: difference known_hosts authorized_keys 
Typescript :: Passing a generic function in as a callback in Typescript 
Typescript :: fs readFile binary 
Typescript :: sourcetree winmerge arguments three way merge 
Typescript :: Name all the elements of set Red. Use the proper set notation. 
Typescript :: typescript default value null or undefined 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =