reactjs - 无法在 App.js 中访问 Redux 状态
问题描述
我需要访问 App.js 中的 redux 状态。
我目前正在使用导航组件“开关”来处理我的应用程序路由,并且我正在使用connect
and 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>
)
}
}
解决方案
我通过创建另一个名为“Root”的组件并在那里拥有我的连接功能来让它工作。
推荐阅读
- refresh - (ncurses) - mvwaddch() 是否调用 wrefresh()?
- cluster-computing - opendaylight 中的集群
- aws-lambda - 在 LD_LIBRARY_PATH (Amazon Linux) 中找不到共享对象
- android - 如何默认启用在锁定屏幕上显示权限
- nativescript - TypeError: this.$refs.scroll.scrollToVerticalOffset 不是函数
- python - 使用 NetworkX 的图之间的相似性度量
- java - 如何使用 AWS Java SDK V2 公开 S3 对象?
- c# - ¿ 如何更正在 Windows 窗体 VS C# 中重复两次的关闭消息?
- com - 在 windbg 脚本中获取 System.__ComObject 的 RCW 值
- c - C程序不接受用户输入