React Native-在App.js中使用react-redux进行Firebase身份验证

时间:2019-03-24 23:53:54

标签: reactjs firebase react-native redux react-redux

我在我的应用程序中使用Firebase身份验证和react-redux。我将firebase.auth().onAuthStateChanged添加到了App.js的componentDidMount函数中,以检查整个应用程序中的用户登录状态。我尝试像这样将App.js连接到我的redux操作的函数getUserProfile

App.js

import React from 'react';
import { createStackNavigator, createAppContainer } from 'react-navigation';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux'; 
import reducers from './app/src/reducers';
import ReduxThunk from 'redux-thunk';
import { connect } from 'react-redux';
import { firebase } from './config/config';
import { getUserProfile } from './app/src/actions/index';

...

const AppContainer = createAppContainer(MainStack);

class App extends React.Component {
  constructor(props) {
    super(props);
  }

  componentDidMount = () => {
    var that = this;
    firebase.auth().onAuthStateChanged(async function(user) {
        if (user) {
            await that.getUserProfile(user.uid);
        } else {
            that.navigation.navigate('Auth');
        }
    })
  }

  render() {
    const store = createStore(reducers, {}, applyMiddleware(ReduxThunk));
    return (
      <Provider store={store}>
        <AppContainer/>
      </Provider>
    );
  }
}

const mapStateToProps = state => {
  return {
      user: state.auth.user
  }
}

export default connect(mapStateToProps, {getUserProfile})(App);

我收到错误消息:永久违反:在“ Connect(App)”的上下文中找不到“ store”。要么将根组件包装在中,要么在连接选项中将自定义React上下文提供程序传递给Connect(App),并将相应的React上下文使用者传递给Connect(App)。如何解决此问题?

1 个答案:

答案 0 :(得分:1)

我现在看到了问题。您正在 Provider 组件的父组件上使用 connect

您需要做的是创建一个名为 Main 的新组件,并在其上使用 connect 。您的 App 组件不能使用connect,因为它不是 Provider 的子级,而是父级。

或者换句话说:

App.js

import React from 'react';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux'; 
import reducers from './app/src/reducers';
import ReduxThunk from 'redux-thunk';

...

const store = createStore(reducers, {}, applyMiddleware(ReduxThunk));

class App extends React.Component {
  render() {
    return (
      <Provider store={store}>
        <Main />
      </Provider>
    );
  }
}

export default App;

Main.js

import React from 'react';
import { createStackNavigator, createAppContainer } from 'react-navigation';
import { connect } from 'react-redux';
import { firebase } from './config/config';
import { getUserProfile } from './app/src/actions/index';

...

const AppContainer = createAppContainer(MainStack);

class Main extends React.Component {
  constructor(props) {
    super(props);
  }

  componentDidMount = () => {
    var that = this;
    firebase.auth().onAuthStateChanged(async function(user) {
        if (user) {
            await that.getUserProfile(user.uid);
        } else {
            that.navigation.navigate('Auth');
        }
    })
  }

  render() {
    return (
      <AppContainer />
    );
  }
}

const mapStateToProps = state => {
  return {
      user: state.auth.user
  }
}

export default connect(mapStateToProps, {getUserProfile})(Main);