NativeBase icon indicating copy to clipboard operation
NativeBase copied to clipboard

evaluating 'this.toastInstance._root' Error when using Toast

Open Symous opened this issue 9 years ago • 16 comments

Hi, if I use Toast of native-base in componentWillReceiveProps method it will give me an error 'null is not an object' (evaluating 'this.toastInstance._root') .

the code is : componentWillReceiveProps(){ Toast.show({ text: 'Msg here', position: 'bottom', buttonText: 'OK' }) } I guess the reason is that the life of the componentWillReceiveProps ends before the Toast , is that right ? and is there any way to slove this since I have to give the user a information after he backward from another page ? Thanks.

Symous avatar May 18 '17 12:05 Symous

I have experienced similar error: "Cannot read property _'root' of undefined"

But, it does not happen if using inside Container:

import React, { Component, PropTypes } from 'react';
import { AppRegistry, Text, View } from 'react-native';
import { Container, Content, Toast } from 'native-base';

export default class toast extends Component {
  //
  constructor(props) {
    super(props);
    this.state = {
      count: 0
    };
  }
  //
  componentWillMount () {
    setInterval(
      () => { this.setState({ count: this.state.count + 1 }) }, 
      5000
    );
  }
  //
  render() {
    return (
      <Container>
        <MyContent count={this.state.count} />
      </Container>
      /*
        It happens when not using Container
        <MyContent count={this.state.count} />
      */
    );
  }
}

class MyContent extends Component {
  static propTypes = {
    count: PropTypes.number.isRequired
  };
  //
  componentWillReceiveProps () {
    console.log(this.props.count)
    Toast.show({
      text: 'Wrong password!',
      position: 'top',
      buttonText: 'Okay',
      duration: 3000
    });
  }
  //
  render() {
    return (
      <Text>{ this.props.count }</Text>
    );
  }
}

AppRegistry.registerComponent('toast', () => toast);

from document: Note: For Toast to work, it has to be called inside Container. https://docs.nativebase.io/Components.html#Toast

I hope this helps.

horiuchie avatar May 30 '17 02:05 horiuchie

#819

shivrajkumar avatar May 31 '17 07:05 shivrajkumar

@horiuchie @shivrajkumar Thanks for your help. :-)

Symous avatar May 31 '17 11:05 Symous

how to resolve this

emsihyo avatar Jul 09 '17 12:07 emsihyo

got the samve error, on native-base 2.2.0

Antoine-Mace avatar Jul 11 '17 11:07 Antoine-Mace

Hi, I just want to note that I'm having this issue on my project. I upgrade my RN and native base

    "native-base": "^2.2.1",
    "react": "16.0.0-alpha.12",
    "react-native": "0.46.2",

I have Toast which needs to be call via component or functions. But as @horiuchie said it will work if its inside in the container, but the problem is I cannot put it inside the container. Do I have any alternative solution? Before this is working but now it is not working.

eggybot avatar Jul 13 '17 15:07 eggybot

Any Updates here? Today I updated from 2.1.5 to 2.2.1 and i got this problem also. Having same problem as eggybot :( This issue should be re-opened...

shuffgy avatar Jul 21 '17 18:07 shuffgy

@eggybot @shuffgy With the latest of NativeBase, the use case of Toast and ActionSheet have changed a bit. Check here

shivrajkumar avatar Jul 24 '17 14:07 shivrajkumar

@shivrajkumar thanks, I already apply the changes and it works now, but I just want to report sometimes this error shows on when I tried switching screen/pages from my app, also if the Toast event I call often the error shows.

I tried this on actual Samsung Galaxy S6, iPhone 6S, iPhone 6, Samsung Galaxy S7 and Oneplus2 devices. All of this devices shows this error sometimes and the behavior pattern I did when the error show on randomly when I'm changing screen/pages.

eggybot avatar Jul 24 '17 18:07 eggybot

Maybe adding Root can help you https://github.com/GeekyAnts/NativeBase/issues/1121#issuecomment-345891604

azat-co avatar Nov 21 '17 01:11 azat-co

Please check this once with NativeBase 2.3.3

SupriyaKalghatgi avatar Nov 22 '17 05:11 SupriyaKalghatgi

My solution here might help you.

kesiena115 avatar Nov 22 '17 06:11 kesiena115

@azat-co I have wrapped my Root with NativeBase root, but the issue still exists (sometimes), using the latest version of NativeBase

IsracardTeam avatar Feb 25 '18 09:02 IsracardTeam

Follow Docs for help, it works

SupriyaKalghatgi avatar Feb 26 '18 06:02 SupriyaKalghatgi

I read the docs, and I found this solution. For Toast to work, you need to wrap your topmost component inside from native-base.

<Root>
    <AppNavigator />
</Root>

ahmedmenaem avatar Mar 14 '18 21:03 ahmedmenaem

@ahmedmenaem I'm using react-native-router-flux and redux-persit libraries. I've configured my root compenent render method as follows:

       <Root>
            <StatusBar
                backgroundColor='#D3C030'
                barStyle='light-content'
                translucent={true}
            />
            <Provider store={this.state.store}>
                <PersistGate
                    loading={<SplashScreen />}
                    onBeforeLift={this.onBeforeLift}
                    persistor={persistor}>
                    <AppRouter />
                </PersistGate>
            </Provider>
        </Root>

But the error still persist. Am I missing something?

michelebombardi avatar Apr 13 '18 11:04 michelebombardi