Firebase检查是否存在用户会话
我试图查看用户会话是否已经存在,以及它是否没有呈现不同的按钮选项Firebase检查是否存在用户会话,firebase,react-native,firebase-authentication,dynamic-content,Firebase,React Native,Firebase Authentication,Dynamic Content,我试图查看用户会话是否已经存在,以及它是否没有呈现不同的按钮选项 render () { var actionButton = []; firebaseApp.auth().onAuthStateChanged(function(user) { if (user) { actionButton.push( <RoundedButton onPress={() => { NavigationActions.SubmitScreen() }} text='Plac
render () {
var actionButton = [];
firebaseApp.auth().onAuthStateChanged(function(user) {
if (user) {
actionButton.push(
<RoundedButton onPress={() => { NavigationActions.SubmitScreen() }} text='Place Order' />
)
} else {
actionButton.push(
<RoundedButton onPress={() => { NavigationActions.Login({hide: false}) }} text='Login to Place Order' />
)
}
})
return (
<View style={styles.container}>
<View style={styles.currentBalance}>
<Text style={styles.currentBalanceTitle}>CURRENT BALANCE</Text>
<Text style={styles.currentBalanceAmount}>$0.00</Text>
</View>
<AlertMessage title='Nothing to See Here, Move Along' show={this._noRowData()} />
<View style={styles.heaader}>
<Text style={styles.item}>Item</Text>
<Text style={styles.price}>Price</Text>
</View>
<ListView
contentContainerStyle={styles.listContent}
dataSource={this.state.dataSource}
renderRow={this._renderRow.bind(this)}
enableEmptySections
pageSize={15}
/>
{actionButton}
</View>
)
render(){
var actionButton=[];
firebaseApp.auth().onAuthStateChanged(函数(用户){
如果(用户){
动作按钮(
{NavigationActions.SubmitScreen()}}text='PlaceOrder'/>
)
}否则{
动作按钮(
{NavigationActions.Login({hide:false}}}text='Login to Place Order'/>
)
}
})
返回(
经常项目差额
$0.00
项目
价格
{actionButton}
)
然后我将
{actionButton}
渲染结果,我遇到的问题是没有返回结果将firebase auth listener置于渲染函数中不是一个好主意。因为无论何时更新状态,它都可以再次渲染,这将再次调用firebase auth listener。最佳位置(从我开始)是ComponentDidMount
或constructor
,并相应地更改状态。或者,您可以使用状态管理(如redux
)检查it用户是否登录
请尝试以下代码段:
class Orders extends Component {
constructor(props) {
super(props);
this.state = {
isLoggedIn: false
}
}
componentDidMount() {
let listener = firebaseApp.auth().onAuthStateChanged((user) => {
if (user) {
this.setState({
isLoggedIn: true
})
listener();
} else {
this.setState({
isLoggedIn: false
})
listener();
}
})
}
render() {
return (
<View style={styles.container}>
<View style={styles.currentBalance}>
<Text style={styles.currentBalanceTitle}>CURRENT BALANCE</Text>
<Text style={styles.currentBalanceAmount}>$0.00</Text>
</View>
<AlertMessage title='Nothing to See Here, Move Along' show={this._noRowData()} />
<View style={styles.heaader}>
<Text style={styles.item}>Item</Text>
<Text style={styles.price}>Price</Text>
</View>
<ListView
contentContainerStyle={styles.listContent}
dataSource={this.state.dataSource}
renderRow={this._renderRow.bind(this)}
enableEmptySections
pageSize={15}
/>
{this.state.isLoggedIn
? <RoundedButton onPress={() => { NavigationActions.SubmitScreen() }} text='Place Order' />
: <RoundedButton onPress={() => { NavigationActions.Login({ hide: false }) }} text='Login to Place Order' />
}
</View>
)
}
}
类命令扩展组件{
建造师(道具){
超级(道具);
此.state={
伊斯洛格丁:错
}
}
componentDidMount(){
让listener=firebaseApp.auth().onAuthStateChanged((用户)=>{
如果(用户){
这是我的国家({
伊斯洛格丁:是的
})
监听器();
}否则{
这是我的国家({
伊斯洛格丁:错
})
监听器();
}
})
}
render(){
返回(
经常项目差额
$0.00
项目
价格
{this.state.isLoggedIn
?{NavigationActions.SubmitScreen()}text='PlaceOrder'/>
:{NavigationActions.Login({hide:false}}}text='Login to Place Order'/>
}
)
}
}
您应该取消从Firebase auth listener的订阅,以避免不必要的行为。它不是在调用回调函数吗?还是不渲染?我说不渲染我会更新问题