evaluating 'this.toastInstance._root' Error when using Toast
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.
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.
#819
@horiuchie @shivrajkumar Thanks for your help. :-)
how to resolve this
got the samve error, on native-base 2.2.0
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.
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...
@eggybot @shuffgy With the latest of NativeBase, the use case of Toast and ActionSheet have changed a bit. Check here
@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.
Maybe adding Root can help you https://github.com/GeekyAnts/NativeBase/issues/1121#issuecomment-345891604
Please check this once with NativeBase 2.3.3
My solution here might help you.
@azat-co I have wrapped my Root with NativeBase root, but the issue still exists (sometimes), using the latest version of NativeBase
Follow Docs for help, it works
I read the docs, and I found this solution.
For Toast to work, you need to wrap your topmost component inside
<Root>
<AppNavigator />
</Root>
@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?