Derive chat state from props [#233].#235
Derive chat state from props [#233].#235leeschumacher merged 1 commit intojkk:masterfrom beejunk:issue-233
Conversation
|
|
||
| _chatScrollRef: ?HTMLElement; | ||
|
|
||
| /* componentWillReceiveProps(nextProps: Props) { |
There was a problem hiding this comment.
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.
return [] in this case to match previous behavior?
There was a problem hiding this comment.
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.
A section of the code was commented out that was responsible for updating the
chatSectionsstate depending on the incoming props. This kept all messages in the chat from displaying, sincechatSectionsis an empty array on initial render and would never get re-computed.The original code used a method called
componentWillReceivePropsthat has since be renamed toUNSAFE_componentWillReceiveProps, which is probably the reason it was commented out. This PR uses the static methodgetDerivedStateFromPropsto adjust thechatSectionsstate in a safe manner. It only updates the state if it detects a difference in the number of messages displayed.This resolves issue #233