@echoing-sugar-95154 Please check code I have added it in gist snippet:
https://gist.github.com/adi/0e4ef3ae4235e61ea45c5b0f01calso added comments for better understanding.
Main reason for adding this if condition -> if(defaultSelectedUserId && (defaultSelectedUserId !== prevValue)) is because:
In clickHandler and useEffect both are dispatching same action which updates redux state but when some user clicks on webpage. Code inside click handler gets executed but important point to note here is that onSelectUseris also dependency in useEffect so again same action is getting dispatched but this time it is setting some different value. So thats why added that if condition inside useEffect which prevents dispatching action if click handler is called.
But my question is how to avoid infinite loop in useEffect as defaultSelectedUserId and prevValue both are present in dependency array and when prop change i.e defaultUserId that time it goes in infinite loop. Please suggest how can I restructure my code or any other suggestions to solve this problem. Thanks