We're using component ChannelList and passing some custom event handlers (eg. onMessageNewHandler). That component uses hook useMessageNewListener to subscribe to message.new event and run some internal logic or to run custom event handler. Internally, hook uses useEffect to manage event subscription but since customHandler prop is not in effect's dependecies, it never updates it so we're always stuck with the inital event callback version which then contains stale component state.
To Reproduce
Steps to reproduce the behavior:
Create a React component
Make a custom callback event function that depends on some local state that changes over time and log that state in console
Use ChannelList component and pass it that event handler in onMessageNewHandler property
Console log that state from component
Trigger message.new event
See that event handler logs the first state value and not the latest one
Expected behavior
When an event handler for ChannelList.onMessageNewHandler changes (eg. because useCallback() dependecies chaged), component should update and use the latest handler version on next appropriate event.
Package version
stream-chat-react: 11.23.10, 12.0.0
Desktop (please complete the following information):
Describe the bug
We're using component
ChannelList
and passing some custom event handlers (eg.onMessageNewHandler
). That component uses hookuseMessageNewListener
to subscribe tomessage.new
event and run some internal logic or to run custom event handler. Internally, hook usesuseEffect
to manage event subscription but sincecustomHandler
prop is not in effect's dependecies, it never updates it so we're always stuck with the inital event callback version which then contains stale component state.To Reproduce
Steps to reproduce the behavior:
ChannelList
component and pass it that event handler inonMessageNewHandler
propertymessage.new
eventExpected behavior
When an event handler for
ChannelList.onMessageNewHandler
changes (eg. becauseuseCallback()
dependecies chaged), component should update and use the latest handler version on next appropriate event.Package version
Desktop (please complete the following information):
Additional context
v12.0.0 - https://github.com/GetStream/stream-chat-react/blob/master/src/components/ChannelList/hooks/useMessageNewListener.ts#L51
v11.23.10 - https://github.com/GetStream/stream-chat-react/blob/v11.23.10/src/components/ChannelList/hooks/useMessageNewListener.ts#L51