0

I was checked everything, All sections are working good but when I triggered a dispatch ,their dependencies invoked two time!

Bellow is my reducer function :

recieveMessage(
  state,
  { payload: { message, topic } }: PayloadAction<IWebSocket>,
) {
  console.log('Recive Message from websocket ', message, topic)
  state.recive = { message, topic }
},

and useEffect

const { socketReducer: { recive } } = useSelector(RootSelector);

useEffect(() => {
    recive && console.log("Fetch from redux")
}, [recive])

Why it fired two time?

  • 2
    Does this answer your question? [Why useEffect running twice and how to handle it well in React?](https://stackoverflow.com/questions/72238175/why-useeffect-running-twice-and-how-to-handle-it-well-in-react) – Vin Xi Dec 14 '22 at 13:27
  • unfortunately not –  Dec 14 '22 at 13:39
  • 1
    Why not? Why doesn't it apply to your question? From all the information you have provided so far, the first answer is also the answer to your question. – Codo Dec 14 '22 at 14:59
  • I wonder why , It wont work –  Dec 14 '22 at 16:38
  • React - in dev mode - deliberately calls all hooks twice. It's by design. It's mentioned in the linked question. It's mentioned in the React documentation. – Codo Dec 14 '22 at 22:18
  • It fired when i listen to redux state –  Dec 15 '22 at 11:47

0 Answers0