react-native-bottom-sheet: [Bug]: Android+iOS TextInput Keyboard lagging BottomSheetModal - Reanimated error

Version

v5

Reanimated Version

v3

Gesture Handler Version

v2

Platforms

Android

What happened?

Keyboard is lagging - Meanwhile this is logging in the console:

Reanimated] Reading from `value` during component render. Please ensure that you do not access the `value` property or use `get` method of a shared value while React is rendering a component.

If you don't want to see this message, you can disable the `strict` mode. Refer to:
https://docs.swmansion.com/react-native-reanimated/docs/debugging/logger-configuration for more details.

Code: const [text, setText] = useState("")

      <BottomSheetModal ref={bottomSheetModalRef} index={1} snapPoints={snapPoints} enablePanDownToClose enableDismissOnClose >
        <BottomSheetView>

          <BottomSheetTextInput
            placeholder='Text...'
            value={text}
            onChangeText={setText} 
          />
                
        </BottomSheetView>
      </BottomSheetModal>

-> The error only occurs if the TextInput is connected to a useState. If I remove this: value={text} onChangeText={setText} everything is normal.

https://github.com/user-attachments/assets/0e7359b0-ea6f-466d-b2f2-311edb0282fd

Reproduction steps

Use the code above on Android with BottomSheetModal

Reproduction sample

https://snack.expo.dev/UwhwaCo1GT5m3B_F9dYwX

Relevant log output

WARN  [Reanimated] Reading from `value` during component render. Please ensure that you do not access the `value` property or use `get` method of a shared value while React is rendering a component.

If you don't want to see this message, you can disable the `strict` mode. Refer to:
https://docs.swmansion.com/react-native-reanimated/docs/debugging/logger-configuration for more details.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 8
  • Comments: 48

Most upvoted comments

Hey I just fixed this using Direct Manipulation

  const bottomSheetTextInputRef = useRef<TextInput>(null)

  const handleChangeText = (text: string) => {
    bottomSheetTextInputRef.current?.setNativeProps({ value: text })
    onChangeText(text) // this is because you may need to access this value from parent
  }

return (
<BottomSheetModal ...>
          <BottomSheetTextInput
            ref={bottomSheetTextInputRef}
            onChangeText={handleChangeText}
            {...inputProps}
          />
</BottomSheetModal ...>
)

I found a way to fix the flickering issue. So basically we know that the issue is only with the “value” prop. So instead of using it, you need to use defaultValue and ALSO the “key” prop.

Why the key prop ? Let’s imagine we want to change the value of the defaultValue after pressing a button or selecting another component that should have updated the value of the inputText. When using defaultValue, it’s impossible to refresh the value unless… we use the ‘key’ property to reload the inputText and update the text in the inputText

Example :

  const [refreshTextInput, setRefreshTextInput] = useState(0);
  const handleUncontrolledReset = () => {
    setRefreshTextInput(prev => prev + 1);
  };

<TextInput
              key={refreshTextInput}
              label="Titre"
              style={styles.input}
              defaultValue={titre}
              mode='outlined'
              onChangeText={newTitre => setTitre(newTitre)}
 />

when I select a cards that I created, my inputText need to updates the text linked to the card. So I use the following example to reload the inputText

  useEffect(() => {
    if (selectedItem) {
      handleUncontrolledReset(); //Reload the textInput with the new value
    }
  }, [selectedItem])

And then No flikering issue and i can refresh the inputText normaly

The error still occurs.

drop-in replacement for the TextInput, based the solution from @nowayX

export default function MyTextInput(props: TextInputProps) { const { value, defaultValue, …rest } = props

return ( <TextInput defaultValue={value ?? defaultValue ?? ‘’} {…rest} /> ); }

This worked for me, thank you 🎉

I just replaced the value prop with the defaultValue prop

<TextInput
     style={Style.input} 
     value={value} // => delete this
     defaultValue={value} // => add this
     onChangeText={e => onChangeText(e)}
    placeholder="Search"
    placeholderTextColor="#9F9E9B"
/>

Create a TextInput component

  import {BottomSheetTextInput} from '@gorhom/bottom-sheet';
  import {useEffect, useState} from 'react';

 const RenderTextInputComponent = props => {
   const [searchText, setSearchText] = useState('');

   useEffect(() => {
     setSearchText(props?.inputValue || '');
    }, [props?.inputValue]);
    return (
       <BottomSheetTextInput
         onChangeText={e => {
         setSearchText(e);
         props?.onChangeTextValue?.(e);
         }}
        value={searchText}
        {...props}
      />

       );
}

export default RenderTextInputComponent;

its work for me

Thanks @geronimosuarez , your solution works! Important to note is that it only works when I use defaultValue and not value in the instatiation of my input component.

Yep! you shouldn’t use the prop value anymore or at least make sure to keep that initial value outside of react. I think this is not an issue with the library itself, it just can happen when the react tree is not completely updated and you send a new update

i’m using your solution here and it works, thanks! but i’ve noticed that the keyboard dismisses first, then the bottom sheet, then the backdrop, in this exact order. I wish it was all one smooth action, are you also encountering this on your end?

@rochajulian I’m experiencing this indeed, especially for the backdrop however If you’d remove await before KeyboardController.dismiss() it should be a little bit better.

You’re right, thanks! I’m happy enough with this solution until Gorhom addresses it. Thanks again!

I just left the house, but before leaving I noticed that if I comment out the “BottomSheetBackdrop”, meaning I disable it, the input works fine. I hope this helps as a temporary solution until @gorhom can address it.

drop-in replacement for the TextInput, based the solution from @nowayX

export default function MyTextInput(props: TextInputProps) {
  const { value, defaultValue, ...rest } = props

  return (
    <TextInput
      defaultValue={value ?? defaultValue ?? ''}
      {...rest}
    />
  );
}

@nowayX works for me. Thank you! 🙏

Same issue for me but only if i put TextInput in

<BottomSheetModal> and <BottomSheetView>

Even if it’s gorhom or default react textInput