NativeBase icon indicating copy to clipboard operation
NativeBase copied to clipboard

Unhandled JS Exception: Cannot read property '_root' of null

Open robinv8 opened this issue 9 years ago • 48 comments

Actionsheet error
Actionsheet.js:94:79

robinv8 avatar Apr 12 '17 08:04 robinv8

@tranren it seem to work fine .Please share the code snippet and the version of NB ,React and RN that you are using .

geekrishabh avatar Apr 13 '17 14:04 geekrishabh

This is my code:

ActionSheet.show(
    {
        options: [
            'Button 1',
	    'Button 2',
	    'Close',
        ],
        cancelButtonIndex: 2,
        title: 'Options',
    },
    (index) => {
        this.props.navigator.push({
            id: 'router1',
        });
    },
);

The first time that I call the ActionSheet.show(), it's work fine.

But, when the ActionSheet is closed, and is called for the second time ActionSheet.show(), this error occurs.

error

claudioscheer avatar Apr 18 '17 14:04 claudioscheer

Have exactly the same problem, the crash only occurs after navigating (push), going back and hitting ActionSheet.show again. Tested on iOS.

Edit: Replacing the native-base wrapper and using the react-native ActionSheetIOS instead solves the problem. However, as the name suggests, ActionSheetIOS only works on iOS...

[email protected] [email protected] [email protected]

joaofranca avatar Apr 18 '17 14:04 joaofranca

@joaofranca Yes, I'm getting the exact error Cannot read property "_root" of null. The first time it works fine, but it throws the error after navigating back to the page from the actionsheet. I'm testing it on an Android emulator.

laukaichung avatar Apr 18 '17 17:04 laukaichung

@GeekRishabh Same error at that line Actionsheet.js:94:79 on Android 5.0 emulator. Open the action sheet , close, open again and get this error:

Image of Error

laukaichung avatar Apr 24 '17 09:04 laukaichung

I'm getting the same error with the Toast. I'm in a screen where after a process is successfully, the application pops back to the previous screen and shows a Toast. When showing that toast I'm getting the error.

Versions: native-base: 2.1.2, react-native: 0.43.3

screen shot 2017-04-25 at 7 29 22 pm

Thanks in advance!

caroaguilar avatar Apr 26 '17 01:04 caroaguilar

I'm having the same error. I can open and close an ActionSheet as many times as I want, but when I navigate to another scene and then come back, the ActionSheet throws the error "null is not an object (evaluating 'this.actionSheetInstance._root')".

simulator screen shot apr 27 2017 1 55 27 pm

BrianBelhumeur avatar Apr 27 '17 20:04 BrianBelhumeur

I was having this issue in an app based on https://github.com/start-react/native-starter-kit which plays animations between different pages in the app. Each page had its own container, which worked fine in past versions of the starter-kit, but is now breaking because there are animations in which both containers are briefly onscreen. Because of this, the timeline is:

Container 1 Mounts              Unmounts
|-------------------------------|
                            |-----------------------...
                            Container 2 Mounts

As a component unmounts in React, it runs its ref function with 'null'. This is causing the error that we're seeing in this thread, since there is a single class of ActionSheet with an instance bound to it, and the animations between pages mean that the unmount is the most recent ref-defining event, setting it to null.

The fix in my case was to wrap the app in a single container that lives above the AppNavigator and replace the per-page containers with views. A little bit of styling later and the app was back to looking the same and the ActionSheet was working as expected! Hope this is helpful for some of the other people having this issue as well. I'm not sure that there is a good solution to make this more robust within NativeBase, but if there is that would be fantastic.

TomMahle avatar Apr 28 '17 04:04 TomMahle

The problem seems to be in NativeBase/src/basic/Container.js, specifically line 16 where the singleton instance is set: <ActionSheet ref={ (c) => {ActionSheet.actionsheetInstance = c;}} />. I got around this by setting up my own ActionSheet in the View instead of accessing the singleton instance.

Something like this:

import React, { Component } from 'react';
import { View } from 'react-native';
import { ActionSheet, Button, Text } from 'native-base';

class Foo extends Component {
    constructor(props) {
         super(props);
         this.actionSheet = null;
    }

    render() {
         return (
             <View>
                 <Button onPress={() => this.showActionSheet()}>
                     <Text>Action Sheet!</Text>
                 </Button>
                 <ActionSheet ref={(c) => { this.actionSheet = c; }} />
             </View>
         );
    }

    showActionSheet() {
        if ( this.actionSheet !== null ) {
            // Call as you would ActionSheet.show(config, callback)
            this.actionSheet._root.showActionSheet({options: [1,2,3]}, (i) => console.log(i));
        }
    }
}

wkirby avatar May 03 '17 16:05 wkirby

Having the same exact issue here when calling Toast.show, navigating to another view, navigating back, then calling Toast.show again.

seanmadi avatar Jun 06 '17 19:06 seanmadi

Fixed with 2.1.5

sankhadeeproy007 avatar Jun 09 '17 14:06 sankhadeeproy007

I was still getting this error after updating to 2.1.5, it seems like the component I am using the actionsheet on is only just getting unmounted right before it gets mounted again and the check in Container still thinks it exists.

I managed to get around it by making sure ActionSheet.actionsheetInstance is null when the component unmounts.

public componentWillUnmount () {
    ActionSheet.actionsheetInstance = null;
}

Edit: Actually that didn't fix fully fix my problem, as the components are not being unmounted so I couldn't use Toast on the login if I started on the main screen. Instead I'm setting the actionsheetInstance and toastInstance to null in the willMount of both the login and main screens.

public componentWillMount() { 
    Toast.toastInstance = null;
    ActionSheet.actionsheetInstance = null;
 }

JaydenR avatar Jun 14 '17 06:06 JaydenR

27166207-cd384f3c-51b6-11e7-9116-bdb43ba8c431

"dependencies": {
    "native-base": "^2.1.5",
    "react": "16.0.0-alpha.12",
    "react-native": "0.45.1"
  },

@claudioscheer

SupriyaKalghatgi avatar Jun 15 '17 06:06 SupriyaKalghatgi

Is there any fix for this? My RN Version is 0.44.0 and native-base 2.1.5

pankaj-ag avatar Jun 15 '17 17:06 pankaj-ag

We have again tested it out on our projects again and it is working. Somebody please link me to a repo where I can replicate it. Will be considered resolved otherwise.

sankhadeeproy007 avatar Jun 19 '17 10:06 sankhadeeproy007

It's working now. Thanks

pankaj-ag avatar Jun 19 '17 14:06 pankaj-ag

I'm still facing this issue on NB version 2.2.0 and react-native: 0.44.0. It works perfectly first time. Then when I open the app again, it fails.

nimeetshah0 avatar Jul 31 '17 04:07 nimeetshah0

I get undefined is not an object (evaluating 'this.actionsheetInstance._root')

React: 16.0.0-alpha.12 React native: 0.46.4 Native base: 2.3.1

MY code:

ActionSheet.show({
   ...
});

peterchibunna avatar Aug 01 '17 08:08 peterchibunna

@peterchibunna I got the same issue like you. But it was work properly previously. Now not. Is this affected from another 3rd party library?

dinithminura avatar Aug 03 '17 09:08 dinithminura

@dinithminura it stopped working when I upgraded nativebase module in my application. I can't say it's due to a 3rd party lib.

peterchibunna avatar Aug 03 '17 09:08 peterchibunna