【问题标题】:Can't Access Redux State in App.js无法在 App.js 中访问 Redux 状态
【发布时间】:2018-07-27 16:05:34
【问题描述】:

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

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

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

App.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>
    )

  }
    }

【问题讨论】:

  • mapStateToProps 用 props 映射状态,实际上需要将 store 传递给 App 组件。否则mapStateToProps 不会知道任何关于状态的信息。
  • Switch 在哪里?

标签: reactjs react-native redux redux-persist


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-10-07
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    • 2020-07-15
    • 2018-06-26
    • 2020-06-17
    • 2023-03-23
    • 2020-10-26
    相关资源
    最近更新 更多