我在 index.js 中有以下结构:
<MyAppBar // + properties/>
<Route path={path} component={Login}/>
MyAppBar.js
class MyAppBar extends React.Component {
state = {
auth: false,
anchorElement: null
};
handleLogin(){
this.setState({auth:true});
}
//another code
//end component
export default withStyles(styles)(MyAppBar)
我想从 Login 组件操作处理程序中将 MyAppBar 状态 auth 标志更新为 true(即handleLogin),但我不知道如何。
我尝试在登录页面上导入MyAppBar(所有组件),但它说
MyAppBar.handleLogin();
不是一个函数...谢谢!
谢谢!
最佳答案
我认为对于这个简单的范例,您不需要经历设置 redux 的所有麻烦(我喜欢 Redux,但有时我们不必使用它)
以下是如何使用新的上下文
来实现它。
It's farily simple and nice to use. Check it here Let me know if that isn't clear.
关于javascript - 从另一个组件 React js 设置状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52853420/