[英]TypeError: undefined is not an object, react native
我向我的应用程序添加了一个新状态以了解用户是否已登录,现在在运行时会显示此错误!
在添加这个之前,一切都很好,一切正常,我尝试寻找这个错误,但每个人都提到了一些我不使用的 babel-react。 正如我提到的清除缓存没有帮助。 我最近没有升级react native,所以不是因为更新。 ESLint 根本不显示错误。 主要问题是这个'isloggedin' 布尔状态wariable,但我不知道问题可能是什么。 任何形式的建议或帮助都会很棒。 提前致谢。
我试过清理缓存,重新启动一切
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;
您可以通过两种方式使用状态作为条件
第一的,
renderContent() {
if (this.state.isloggedin) {
return (
<Button>
Log Out
</Button>
)
} else
return <Loginform />;
}
第二,
renderContent() {
const{isloggedin}=this.state
if (isloggedin) {
return (
<Button>
Log Out
</Button>
)
} else
return <Loginform />;
}
它看起来像你正试图采取现场isloggedin
从对象this.user
。 但是,没有这样的对象,您在状态中保存了该属性。 所以它可以通过下一个方式修复:
renderContent() {
if (this.state.isloggedin) {
return (
<Button>
Log Out
</Button>
)
} else
return <Loginform />;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.