Redux problem explaination if possible πŸ˜„

Please consider the code below:

const reducer = (state="initial", action) => {
  return (
    action.type === "LOGIN"
    ? "LOGGING IN"
    : "changed"
const store = Redux.createStore(reducer);
console.log(store.getState()); //changed

Why the state changed, even though there is no action dispatched.

Redux will run all your reducers for the first when your code loads.
You set your initial state to 'initial'. And when your code runs for the first time you return in the else statement of your tenary operator the string 'changed'.
You should have instead returned
state in your else case; which will be the string 'initial'
Nothing weird

1 Like

Nothing magic :)). In my first job, I had to deal with PHP which has dynamically created methods and I could not grasp the concept, I wasn’t able to find the definition of a specific method.
I asked my lead developer at that time, how does that work and he said: β€œMagic”

The Redux action has an initial value set by default by Redux, when you create the store, the reducer runs, it checks the action type(the action type in this case is an initialized arbitary value set by Redux), it finds that action.type !== "LOGIN", so it returns "changed", thus the state get changed to "changed".