0

I would like to create a reducer that updates the value of a variable.

So far I have this but I don't know which code to use to update the value:

    import { createSlice } from '@reduxjs/toolkit';

    const initialStateValue = {
        value: ''
    }

    const benutzerSlice = createSlice({
        name: 'user',
        initialState: initialStateValue,
        reducers: {
            changeValue: (state = initialStateValue) => {
               state.value;
            },
        }
    });

    export const {changeValue} = userSlice.actions;
    export default userSlice.reducer;
CodingNoob28
  • 23
  • 1
  • 6
  • How do you want to update the value? Does your chageValue action receive a payload describing the change? Or is it always changed in the same way? – Chad S. May 16 '22 at 16:14
  • I have an input field in one of my screens where the user can type in the username and submit it via a button. – CodingNoob28 May 16 '22 at 16:17
  • And is the value that the user submits passed in the action payload? – Chad S. May 16 '22 at 16:18
  • I unfortunately don't know how to do that. – CodingNoob28 May 17 '22 at 09:10
  • I would highly recommend walking through a simple tutorial on react and redux until you're familiar with the basic concepts. When you call dispatch with your action creator, are you passing the input the user submits? e.g. `dispatch(changeValue(userValue))` – Chad S. May 17 '22 at 16:06
  • I have to admit I struggle a bit with React, I'll look into it again. `dispatch(changeValue(userValue)`is indeed what I would've done, my problem is that I don't know which code to use for the reducer. I have a similar situation where I set the temperature and the code I use in the reducer is `increaseTemp: (state = initialStateValue) => {state.value++}` (and -- for decreaseTemp) and I already worked out how to use it in the screen (`dispatch(increaseTemp())`). I just don't know how to set a new value for the username within the reducer so that I can use changeValue in the screen. – CodingNoob28 May 18 '22 at 09:00
  • I answered a similar question here.. Perhaps it will help you further your understanding: https://stackoverflow.com/questions/72276113/problem-with-expected-0-arguments-but-got-1-in-redux-toolkit/72280584#72280584 – Chad S. May 18 '22 at 13:27
  • That helps me a lot, thanks! – CodingNoob28 May 18 '22 at 19:19

0 Answers0