#redux not updating state

9 messages · Page 1 of 1 (latest)

safe dust
#

ok so im incredibly new to redux (watched like 1 tutorial) i got the basic counter example working but now i am stuck trying to make a custom reducer

this is the reducer (yes it is in my store)

import { createSlice } from '@reduxjs/toolkit'
import type { PayloadAction } from '@reduxjs/toolkit'
import type { RootState } from '../store.ts'
import type { Module } from '../../assets/moduleList.tsx'
import modulesList from '../../assets/moduleList.tsx'

// Define the initial state using that type
const initialState: Module = modulesList[0];

export const currentModuleSlice = createSlice({
    name: 'currentModule',
    // `createSlice` will infer the state type from the `initialState` argument
    initialState,
    reducers: {
        setCurrent: (state, action: PayloadAction<Module>) => {
            state = action.payload
        }
    },
})

export const { setCurrent } = currentModuleSlice.actions

export default currentModuleSlice.reducer

and this is my code that should set it```html
<button
onClick={(e) => {
console.log(currentModule.name)
console.log(m.name)
dispatch(setCurrent(m)) // dispatch = useAppDispatch (for typescript types)
console.log(currentModule.name)
}}
>
{
m.iconPath ?
<img src={m.iconPath} alt="" />
: null
}
{m.name}
</button>


and the console output from this is ```
Tasks (Default)
Profiles (Clicked)
Tasks (Default)```
#

idk what im doing wrong tbf lol ive just edited the code for the counter a little bit

safe dust
#

pls ping me if u respond btw, thanks

safe dust
#

can anyone help?

safe dust
#

forgot to add the currentModule in the second codeblock is defined as const currentModule = useAppSelector((state) => state.currentModule)

outer stone
#

have you fixed the issue? It's been some time since I used redux, so I don't remember much 😅

safe dust
#

looked at it a few times since last night but can't see anything wrong or fix it

safe dust
#

still cant fix btw