首页 > 解决方案 > 无法在 App.js 中访问 Redux 状态

问题描述

我需要访问 App.js 中的 redux 状态。

我目前正在使用导航组件“开关”来处理我的应用程序路由,并且我正在使用connectand mapStateToProps,它允许我在mapStateToProps函数中查看我的应用程序状态。这个问题是 App.js 中的 this.props 不会返回任何状态。

我正在尝试通过我的 redux 状态将 isAuthenticated 传递给 Switch。

应用程序.js:

function mapStateToProps(state) {
  console.log(state.token)
  return{
    token: state.token
  }

}

let Container = connect(mapStateToProps, null)(Switch(this.props.token.isAuthenticated))


export default class App extends React.Component {

  constructor(props) {
    super(props);

    this.state = {
      signedIn: false,
      checkedSignIn: false,
    };
  }

    render () {
      console.log('props')
      console.log(this.props)

      const { checkedSignIn, signedIn } = this.state;


    return (
      <Provider store={store}>
        <PersistGate loading={null} persistor={persistor}>
          <Container />
        </PersistGate>
      </Provider>
    )

  }
    }

标签: reactjsreact-nativereduxredux-persist

解决方案


我通过创建另一个名为“Root”的组件并在那里拥有我的连接功能来让它工作。


推荐阅读