1

In reduxSlice file, I am getting an error - 'curentamount' is not defined no-undef.

Redux slice:

import { createSlice } from '@reduxjs/toolkit'

export const addtoSlice = createSlice({
  name: 'addto',
  initialState: {
    curentamount: 0,
  },
  reducers: {
    incriment: (state) => {
      state.curentamount = curentamount + 1
    },
    decriment: (state) => {
      state.curentamount = curentamount - 1
    },
  },
})

export const { incriment, decriment } = addtoSlice.actions

export const selectadto = (state) => state.addtoSlice.curentamount

export default addtoSlice.reducer

React Component:

import React from 'react'
import addList from './features/auth/ads.json'

const Addtocart = () => {
  return (
    <div>
      {addList.map((add) => (
        <div key={add.id}>
          <div> {add.addname}</div>
          <div> {add.price}</div>
        </div>
      ))}
    </div>
  )
}

export default Addtocart

Error Message:

Failed to compile.

src/features/auth/addtoSlice.js
  Line 11:34:  'curentamount' is not defined  no-undef
  Line 14:34:  'curentamount' is not defined  no-undef
Ajeet Shah
  • 18,551
  • 8
  • 57
  • 87
famo
  • 160
  • 1
  • 13
  • @AjeetShah ,it worked tq, can you put this as the answer so I can choose your answer – famo Mar 20 '21 at 11:44
  • 1
    I added an answer. Isn't there a spelling mistake in "incriment" and "curentamount"? It should be "currentAmount" and "increment". Not important but it makes code more readable. – Ajeet Shah Mar 20 '21 at 11:55
  • 1
    @AjeetShah yes there is a mistake, tq for pointing it out ,I updated it, – famo Mar 20 '21 at 12:50
  • @AjeetShah updated another question , if possible please check, can check in my profile – famo Mar 20 '21 at 14:12
  • @AjeetShah , updated a question in my wall please check if possible tq – famo Mar 21 '21 at 12:52
  • @AjeetShah added a new question would you be able to check it ? – famo May 19 '21 at 14:26

1 Answers1

2

curentamount is not defined at line state.curentamount = curentamount + 1. You can write any of these:

state.curentamount++
state.curentamount += 1
state.curentamount = state.curentamount + 1

Or, read changeBy value from the payload (if you are sending it):

incriment: (state, action) => {
  state.curentamount += action.payload.changeBy
},

An example to send changeBy in payload:

// in a component
useEffect(() => {
  dispatch(incriment({ changeBy: 10 }))
}, [dispatch])
Ajeet Shah
  • 18,551
  • 8
  • 57
  • 87