React-native-paper: Focussing a text input as soon as it is available crashes the application.

Created on 5 Sep 2018  路  6Comments  路  Source: callstack/react-native-paper


Current behaviour

I'm trying to give a TextInput focus as soon as the Dialog in which it is contained, is opened. When trying to focus it as soon as the reference is available, the application crashes.

Expected behaviour

I expect the TextInput to focus and open the Keyboard as soon as the dialog opens.

Code sample

The following should reproduce:

<TextInput 
     ref={i => i.focus()} //This crashes.
     name="value" 
     label={this.props.label} 
     value={this.state.value} 
     onChangeText={this.onValueChange} 
     multiline={this.props.multiline} 
     numberOfLines={this.props.numberOfLines} />

What have you tried

Placing it on a timeout, and checking if i itself is null (it is not). After that I was out of ideas pretty quickly.

Your Environment

| software | version
| --------------------- | -------
| android | SDK 26
| react-native | 16.3.1
| react-native-paper | 2.0.1
| node | v8.11.3
| npm or yarn | 6.1.0

Stack Trace

09-05 18:31:16.112 23268 2546 E ReactNativeJS: TypeError: null is not an object (evaluating 'i.focus')
09-05 18:31:16.112 23268 2546 E ReactNativeJS:
09-05 18:31:16.112 23268 2546 E ReactNativeJS: This error is located at:
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in withTheme(TextInput) (at DialogPreferenceText.js:28)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in View (at DialogPreferenceText.js:27)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in View (at ScrollView.js:791)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in RCTScrollView (at ScrollView.js:887)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in ScrollView (at AbstractDialog.js:58)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in View (at AbstractDialog.js:57)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in View (at Surface.js:65)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in Surface
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in ThemeProvider
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in withTheme(Surface) (at createAnimatedComponent.js:154)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in AnimatedComponent (at Dialog.js:114)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in View (at Modal.js:160)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in View (at createAnimatedComponent.js:154)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in AnimatedComponent (at Modal.js:148)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in Modal
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in ThemeProvider
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in withTheme(Modal) (at Dialog.js:113)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in Dialog
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in ThemeProvider
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in withTheme(Dialog) (at AbstractDialog.js:55)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in ThemeProvider (at Portal.js:54)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in View (at PortalManager.js:44)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in PortalManager (at PortalHost.js:134)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in Portal.Host (at App.js:47)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in View (at MenuProvider.js:219)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in View (at MenuProvider.js:218)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in MenuProvider (at App.js:45)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in ThemeProvider (at Provider.js:17)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in View (at PortalHost.js:131)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in Portal.Host (at Provider.js:16)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in Provider (at App.js:44)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in App (at renderApplication.js:33)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in View (at AppContainer.js:102)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in View (at AppContainer.js:122)
09-05 18:31:16.112 23268 2546 E ReactNativeJS: in AppContainer (at renderApplication.js:32)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: TypeError: null is not an object (evaluating 'i.focus')
09-05 18:31:16.126 23268 2546 E ReactNativeJS:
09-05 18:31:16.126 23268 2546 E ReactNativeJS: This error is located at:
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in withTheme(TextInput) (at DialogPreferenceText.js:28)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in View (at DialogPreferenceText.js:27)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in View (at ScrollView.js:791)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in RCTScrollView (at ScrollView.js:887)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in ScrollView (at AbstractDialog.js:58)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in View (at AbstractDialog.js:57)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in View (at Surface.js:65)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in Surface
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in ThemeProvider
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in withTheme(Surface) (at createAnimatedComponent.js:154)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in AnimatedComponent (at Dialog.js:114)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in View (at Modal.js:160)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in View (at createAnimatedComponent.js:154)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in AnimatedComponent (at Modal.js:148)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in Modal
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in ThemeProvider
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in withTheme(Modal) (at Dialog.js:113)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in Dialog
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in ThemeProvider
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in withTheme(Dialog) (at AbstractDialog.js:55)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in ThemeProvider (at Portal.js:54)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in View (at PortalManager.js:44)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in PortalManager (at PortalHost.js:134)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in Portal.Host (at App.js:47)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in View (at MenuProvider.js:219)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in View (at MenuProvider.js:218)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in MenuProvider (at App.js:45)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in ThemeProvider (at Provider.js:17)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in View (at PortalHost.js:131)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in Portal.Host (at Provider.js:16)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in Provider (at App.js:44)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in App (at renderApplication.js:33)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in View (at AppContainer.js:102)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in View (at AppContainer.js:122)
09-05 18:31:16.126 23268 2546 E ReactNativeJS: in AppContainer (at renderApplication.js:32)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: TypeError: null is not an object (evaluating 'i.focus')
09-05 18:31:16.132 23268 2546 E ReactNativeJS:
09-05 18:31:16.132 23268 2546 E ReactNativeJS: This error is located at:
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in withTheme(TextInput) (at DialogPreferenceText.js:28)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in View (at DialogPreferenceText.js:27)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in View (at ScrollView.js:791)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in RCTScrollView (at ScrollView.js:887)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in ScrollView (at AbstractDialog.js:58)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in View (at AbstractDialog.js:57)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in View (at Surface.js:65)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in Surface
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in ThemeProvider
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in withTheme(Surface) (at createAnimatedComponent.js:154)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in AnimatedComponent (at Dialog.js:114)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in View (at Modal.js:160)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in View (at createAnimatedComponent.js:154)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in AnimatedComponent (at Modal.js:148)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in Modal
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in ThemeProvider
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in withTheme(Modal) (at Dialog.js:113)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in Dialog
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in ThemeProvider
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in withTheme(Dialog) (at AbstractDialog.js:55)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in ThemeProvider (at Portal.js:54)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in View (at PortalManager.js:44)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in PortalManager (at PortalHost.js:134)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in Portal.Host (at App.js:47)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in View (at MenuProvider.js:219)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in View (at MenuProvider.js:218)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in MenuProvider (at App.js:45)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in ThemeProvider (at Provider.js:17)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in View (at PortalHost.js:131)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in Portal.Host (at Provider.js:16)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in Provider (at App.js:44)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in App (at renderApplication.js:33)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in View (at AppContainer.js:102)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in RCTView (at View.js:60)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in View (at AppContainer.js:122)
09-05 18:31:16.132 23268 2546 E ReactNativeJS: in AppContainer (at renderApplication.js:32)

Most helpful comment

I thought I would take a look at it myself and offer a PR to fix if I could figure it out. For me this is an implementation issue. In the TextInput example, it initially works fine. When you navigate away and the component is unmounted, the reference is destroyed.

I've not seen a TextInput be focused like that, though it is nice and succinct. If you are to try and focus automatically like that you should have a defensive check to check the component exists.

<TextInput
 ref={i => i && i.focus()}

Personally I would say it wasn't a bug.

All 6 comments

Thanks for the report. Will take a look.

May I ask why you don't use the autoFocus prop?

Hi @satya164 - thanks for the response! To be completely honest with you, I did not know about the autoFocus prop. I'll have a look whether it works in the morning!

Might I ask where you found out about the autoFocus prop? The docs do not seem to mention it, but I always suspected something like it should exist. Could it be that I am looking at a docs page for an earlier version? I am currently looking at https://callstack.github.io/react-native-paper/text-input.html

Thanks in advance for any further assistance!

@TeunKooijman we need to do a better job at our docs :D

Our TextInput component also accepts the same props as React Native's TextInput, which supports an autoFocus prop: https://facebook.github.io/react-native/docs/textinput#autofocus

Ahh, that explains a lot! Thanks for the help, and I'm sorry for wasting your time. I'll leave this issue open for you guys to decide whether you want to look at the "bug" or not. Have a nice day!

I thought I would take a look at it myself and offer a PR to fix if I could figure it out. For me this is an implementation issue. In the TextInput example, it initially works fine. When you navigate away and the component is unmounted, the reference is destroyed.

I've not seen a TextInput be focused like that, though it is nice and succinct. If you are to try and focus automatically like that you should have a defensive check to check the component exists.

<TextInput
 ref={i => i && i.focus()}

Personally I would say it wasn't a bug.

@Aidurber thanks for looking into it. Yeah, it's indeed not a bug. If you want to call methods as soon as the input is rendered, you should do it in componentDidMount, not the ref callback. I missed that part.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sm2017 picture sm2017  路  4Comments

knobandre picture knobandre  路  4Comments

scottybo picture scottybo  路  3Comments

ZhengYuTay picture ZhengYuTay  路  3Comments

ButuzGOL picture ButuzGOL  路  3Comments