[英]TypeError: undefined is not an object, react native
I added a new state to my application to know whether the user is logged in or not, and this error shows up during the runtime now!我向我的应用程序添加了一个新状态以了解用户是否已登录,现在在运行时会显示此错误!
Before adding this, everything was great, everything was working fine, I tried looking for this error but everybody mentioned some babel-react which I don't use.在添加这个之前,一切都很好,一切正常,我尝试寻找这个错误,但每个人都提到了一些我不使用的 babel-react。 As I mentioned clearing cache didn't help.正如我提到的清除缓存没有帮助。 I haven't upgraded react native lately, so it's not because of the update.我最近没有升级react native,所以不是因为更新。 ESLint doesn't show error at all. ESLint 根本不显示错误。 Main problem is with this 'isloggedin' boolean state wariable, but I don't realize what the issue may be.主要问题是这个'isloggedin' 布尔状态wariable,但我不知道问题可能是什么。 Any kind of advice or help would be amazing.任何形式的建议或帮助都会很棒。 Thank's in advance.提前致谢。
I've tried cleaning the cache, restarting everything我试过清理缓存,重新启动一切
import React, { Component } from 'react';
import { View } from 'react-native';
import firebase from 'firebase';
import { Header, Button } from './components/common';
import Loginform from './components/Loginform';
class App extends Component {
state = { isloggedin: false };
componentWillMount() {
firebase.initializeApp({
apiKey: 'AIzaSyDN_m1VetpIBLji_S8Hng1zbPlNwaQTBiI',
authDomain: 'authentication-83b93.firebaseapp.com',
databaseURL: 'https://authentication-83b93.firebaseio.com',
projectId: 'authentication-83b93',
storageBucket: '',
messagingSenderId: '863329573565',
appId: '1:863329573565:web:08bedbdc76995c53'
}
);
firebase.auth().onAuthStateChanged((user) => {
if (user) {
this.setState({ isloggedin: true });
} else {
this.setState({ isloggedin: false });
}
});
}
renderContent() {
if (this.user.isloggedin) {
return (
<Button>
Log Out
</Button>
)
} else
return <Loginform />;
}
render() {
return (
<View>
<Header headerText="HopeSter Log In" />
{this.renderContent()}
</View>
);
}
}
export default App;
You can use the state as condition in 2 ways您可以通过两种方式使用状态作为条件
First,第一的,
renderContent() {
if (this.state.isloggedin) {
return (
<Button>
Log Out
</Button>
)
} else
return <Loginform />;
}
second,第二,
renderContent() {
const{isloggedin}=this.state
if (isloggedin) {
return (
<Button>
Log Out
</Button>
)
} else
return <Loginform />;
}
It looks like you are trying to take field isloggedin
from the object this.user
.它看起来像你正试图采取现场isloggedin
从对象this.user
。 However, there is no such object, you saved that property in the state.但是,没有这样的对象,您在状态中保存了该属性。 So it can be fixed next way:所以它可以通过下一个方式修复:
renderContent() {
if (this.state.isloggedin) {
return (
<Button>
Log Out
</Button>
)
} else
return <Loginform />;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.