-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Derive chat state from props [#233]. #235
Conversation
}; | ||
|
||
_chatScrollRef: ?HTMLElement; | ||
|
||
/* componentWillReceiveProps(nextProps: Props) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the original code that updated the chat. Looks like it's been commented out for a couple years now.
Vercel deployment: https://shinkgs.ectopod.vercel.app |
return { ...state, chatSections: currentChatSections }; | ||
} | ||
|
||
return null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return [] in this case to match previous behavior?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returning null
in this case indicates that the component state shouldn't be changed from whatever it currently may be. It's a requirement of the getDrivedStateFromProps
method: https://reactjs.org/docs/react-component.html#static-getderivedstatefromprops
In other words, it's saying that no new chat messages have come in from the props, so the component state doesn't need to reflect that.
The chatSections
state is being defaulted to an empty array on line 65, so it should always at least be that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, thanks!
A section of the code was commented out that was responsible for updating the
chatSections
state depending on the incoming props. This kept all messages in the chat from displaying, sincechatSections
is an empty array on initial render and would never get re-computed.The original code used a method called
componentWillReceiveProps
that has since be renamed toUNSAFE_componentWillReceiveProps
, which is probably the reason it was commented out. This PR uses the static methodgetDerivedStateFromProps
to adjust thechatSections
state in a safe manner. It only updates the state if it detects a difference in the number of messages displayed.This resolves issue #233