Redux - Return of reducer function

Tell us what’s happening:
Describe your issue in detail here.
Shouldn’t the return of the reducer function be a brand new state? And shouldn’t a state be an object? How come I’m returning just action.text, considering action.text is just a string? Thank you.

  **Your code so far**
const ADD_NOTE = 'ADD_NOTE';

const notesReducer = (state = 'Initial State', action) => {
switch(action.type) {
  // Change code below this line
  case ADD_NOTE:
    return action.text;
    
  // Change code above this line
  default:
    return state;
}
};


const addNoteText = (note) => {
// Change code below this line
return {
  type: ADD_NOTE,
  text: note,
}
// Change code above this line
};

const store = Redux.createStore(notesReducer);

console.log(store.getState());
store.dispatch(addNoteText('Hello!'));
console.log(store.getState());
  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.115 Safari/537.36

Challenge: Send Action Data to the Store

Link to the challenge:

1 Like

Hi @leandro.bcamargo

Though state in real-world applications using Redux will almost always be objects, it doesn’t necessarily have to be. State can be a primitive data type like boolean, string, number e.t.c.

2 Likes

changing the state directly via reducer is not allowed but with redux toolkit we can do so how to change state when we aren’t using redux toolkit your views sir

1 Like