0

I have created a login component, and there is a state Auth with redux

that and reducer to update the Auth state (initally it is set as FALSE)

but on initial render dispatch is called 4 times and state is updated to TRUE

i have tried useMemo, but not sure how exacltly use it.

https://codesandbox.io/s/react-reducer-ijhj5?file=/src/Components/LoginComponent.js

1 Answers1

1

Your code has a bug

<button onClick={() => memoziedLogin}>Login</button>

The memoziedLogin should be called like memoziedLogin(). See below bellow

<button onClick={() => memoziedLogin()}>Login</button>
moshfiqrony
  • 4,303
  • 2
  • 20
  • 29
  • ok @moshfiqrony, but still the same, tried what you said, getting the things rendered on load and dispatched the action and state changed on initial render – Vijayashankar Aruchamy Feb 13 '21 at 05:28
  • Which action is calling? – moshfiqrony Feb 13 '21 at 13:03
  • `const memoziedLogin = useMemo(() => { dispatch(LoginAction()); }, []);` this is the event attached to button `` now even before clicking button , on initial render this memoziedLogin is called which intern LoginAction is called – Vijayashankar Aruchamy Feb 14 '21 at 05:40