Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js redux example

import { createSlice, configureStore } from '@reduxjs/toolkit'

const counterSlice = createSlice({
  name: 'counter',
  initialState: {
    value: 0
  },
  reducers: {
    incremented: state => {
      // Redux Toolkit allows us to write "mutating" logic in reducers. It
      // doesn't actually mutate the state because it uses the Immer library,
      // which detects changes to a "draft state" and produces a brand new
      // immutable state based off those changes
      state.value += 1
    },
    decremented: state => {
      state.value -= 1
    }
  }
})

export const { incremented, decremented } = counterSlice.actions

const store = configureStore({
  reducer: counterSlice.reducer
})

// Can still subscribe to the store
store.subscribe(() => console.log(store.getState()))

// Still pass action objects to `dispatch`, but they're created for us
store.dispatch(incremented())
// {value: 1}
store.dispatch(incremented())
// {value: 2}
store.dispatch(decremented())
// {value: 1}
Comment

PREVIOUS NEXT
Code Example
Javascript :: apollo client nextjs 
Javascript :: create a pdf puppeteer js 
Javascript :: javascript array loop 
Javascript :: python minify json 
Javascript :: nested ternarys javascript 
Javascript :: web3 connect to smart contract 
Javascript :: mongodb find and update one field 
Javascript :: gym open ai 
Javascript :: react build size 
Javascript :: javascript the event loop 
Javascript :: js slice string at word 
Javascript :: get data from excel using vue js 
Javascript :: map in react 
Javascript :: next js css background image 
Javascript :: jquery syntax 
Javascript :: isfinite javascript 
Javascript :: Add remove link dropzone 
Javascript :: set javascript 
Javascript :: how to compare previous value with current in jquery 
Javascript :: execute shell command in javascript 
Javascript :: ajax timer 
Javascript :: json object 
Javascript :: angular multiselect dropdown 
Javascript :: javascript not equal 
Javascript :: javascript do while array 
Javascript :: update object in array state react 
Javascript :: String operators in JavaScript 
Javascript :: leaflet add scale 
Javascript :: headless ui modal 
Javascript :: Show and Hide Content jQuery 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =