为什么我的Redux商店没有获得Firebase电子邮件属性?反应

时间:2019-01-07 00:45:58

标签: javascript reactjs firebase redux

当我尝试使用Firebase登录时,我想在Redux存储中接收用户数据。

当我 alert()函数内的 onAuthStateChanged 中的电子邮件时,我会收到它,但是当我尝试从其他组件中发出警报时,我会收到 undefined

看,这是我的onAuthState

firebase.auth().onAuthStateChanged(user => {
  if (user) {
    store.dispatch({
  type: "USER_LOGIN",
  payload: { user: user.providerData[0] }
});
  } else {
    store.dispatch({ type: "USER_LOGOUT" });
  }
});

这是我的 redux商店

const reducer = (state, action) => {
  switch (action.type) {
    case "USER_LOGIN":
      return { ...state, logged: true };

    case "USER_LOGOUT":
      return { ...state, logged: false };

    default:
      return state;
  }
};

const initialState = {
  logged: null,
  user: []
};

谢谢!

1 个答案:

答案 0 :(得分:0)

您是否已从其他组件连接到redux?

import React from 'react'
Import {connect} from 'react-redux'
...
class YourOtherComponent extends React.Component {

    state = {
         user: ''
    }

    componentDidMount(){
        this.setState({user: this.props.user})
    }

    render(){

        return(

        <div>this.state.user</div>

        )
    }
}
mapStateToProps = state => ({
    user: state.user
})

export default connect(mapStateToProps )(YourOtherComponent)