Skip to content
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

fix: Remove potential race conditions from setState changes #2474

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 32 additions & 32 deletions src/GiftedChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,12 @@ function GiftedChat<TMessage extends IMessage = IMessage>(
useEffect(() => {
isMountedRef.current = true

setState({
...state,
setState((prevState:GiftedChatState) => ({
...prevState,
messages,
// Text prop takes precedence over state.
...(text !== undefined && text !== state.text && { text: text }),
})
...(text !== undefined && text !== prevState.text && { text: text }),
}))

if (inverted === false && messages?.length) {
setTimeout(() => scrollToBottom(false), 200)
Expand Down Expand Up @@ -391,11 +391,11 @@ function GiftedChat<TMessage extends IMessage = IMessage>(

const newMessagesContainerHeight = getMessagesContainerHeightWithKeyboard()

setState({
...state,
setState((prevState:GiftedChatState) => ({
...prevState,
typingDisabled: true,
messagesContainerHeight: newMessagesContainerHeight,
})
}))
}
}

Expand All @@ -408,11 +408,11 @@ function GiftedChat<TMessage extends IMessage = IMessage>(

const newMessagesContainerHeight = getBasicMessagesContainerHeight()

setState({
...state,
setState((prevState:GiftedChatState) => ({
...prevState,
typingDisabled: true,
messagesContainerHeight: newMessagesContainerHeight,
})
}))
}
}

Expand All @@ -421,21 +421,21 @@ function GiftedChat<TMessage extends IMessage = IMessage>(
onKeyboardWillShow(e)
}

setState({
...state,
setState((prevState:GiftedChatState) => ({
...prevState,
typingDisabled: false,
})
}))
}

const onKeyboardDidHide = (e: any) => {
if (Platform.OS === 'android') {
onKeyboardWillHide(e)
}

setState({
...state,
setState((prevState:GiftedChatState) => ({
...prevState,
typingDisabled: false,
})
}))
}

const scrollToBottom = (animated = true) => {
Expand Down Expand Up @@ -506,10 +506,10 @@ function GiftedChat<TMessage extends IMessage = IMessage>(
})

if (shouldResetInputToolbar === true) {
setState({
...state,
setState((prevState:GiftedChatState) => ({
...prevState,
typingDisabled: true,
})
}))

resetInputToolbar()
}
Expand Down Expand Up @@ -541,12 +541,12 @@ function GiftedChat<TMessage extends IMessage = IMessage>(
minComposerHeight,
)

setState({
...state,
setState((prevState:GiftedChatState) => ({
...prevState,
text: getTextFromProp(''),
composerHeight: minComposerHeight,
messagesContainerHeight: newMessagesContainerHeight,
})
}))
}

const onInputSizeChanged = (size: { height: number }) => {
Expand All @@ -559,11 +559,11 @@ function GiftedChat<TMessage extends IMessage = IMessage>(
newComposerHeight,
)

setState({
...state,
setState((prevState:GiftedChatState) => ({
...prevState,
composerHeight: newComposerHeight,
messagesContainerHeight: newMessagesContainerHeight,
})
}))
}

const _onInputTextChanged = (_text: string) => {
Expand All @@ -577,7 +577,7 @@ function GiftedChat<TMessage extends IMessage = IMessage>(

// Only set state if it's not being overridden by a prop.
if (text === undefined) {
setState({ ...state, text: _text })
setState((prevState:GiftedChatState) => ({ ...prevState, text: _text }))
}
}

Expand All @@ -602,13 +602,13 @@ function GiftedChat<TMessage extends IMessage = IMessage>(
minComposerHeight,
)

setState({
...state,
setState((prevState:GiftedChatState) =>({
...prevState,
isInitialized: true,
text: getTextFromProp(initialText),
composerHeight: minComposerHeight,
messagesContainerHeight: newMessagesContainerHeight,
})
}))
}

const onMainViewLayout = (e: LayoutChangeEvent) => {
Expand All @@ -621,13 +621,13 @@ function GiftedChat<TMessage extends IMessage = IMessage>(
) {
maxHeightRef.current = layout.height

setState({
...state,
setState((prevState:GiftedChatState) =>({
...prevState,
messagesContainerHeight:
keyboardHeightRef.current > 0
? getMessagesContainerHeightWithKeyboard()
: getBasicMessagesContainerHeight(),
})
}))
}

if (isFirstLayoutRef.current === true) {
Expand Down