Javascript 将道具传递给导航栏不会更新组件
我正在建立一个广告网站。我建立了一个注册系统,它工作得非常好,但由于某些原因,我无法根据发生的事件更新导航栏。例如,我想用LOGGED-IN替换名为LOGIN/REGISTER的NavLink。我已经将User.ID的道具从父组件App.js传递到其他组件中,没有任何问题,但无法为导航栏执行此操作。如果我尝试一个console.log,它会说未定义。我将放几个代码来演示它在哪里工作,在哪里不工作: APP.JS 例如,在Profile.jsx中,我可以做到: PROFILE.JSX 但当我在菜单组件中尝试时,我无法管理它相应地更新: NAVBAR.JSXJavascript 将道具传递给导航栏不会更新组件,javascript,reactjs,parameter-passing,navbar,Javascript,Reactjs,Parameter Passing,Navbar,我正在建立一个广告网站。我建立了一个注册系统,它工作得非常好,但由于某些原因,我无法根据发生的事件更新导航栏。例如,我想用LOGGED-IN替换名为LOGIN/REGISTER的NavLink。我已经将User.ID的道具从父组件App.js传递到其他组件中,没有任何问题,但无法为导航栏执行此操作。如果我尝试一个console.log,它会说未定义。我将放几个代码来演示它在哪里工作,在哪里不工作: APP.JS 例如,在Profile.jsx中,我可以做到: PROFILE.JSX 但当我在菜单
React将仅在响应新状态或新道具时更新。您正在操作无法导致组件重新渲染的cookie。这里有一个解决方案: 在应用程序组件中,将日志方法更改为:
constructor(){
super();
this.state ={
currentUserId: cookies.get('UserID'),
currentUser: cookies.get('User')
};
this.LogUser = this.LogUser.bind(this);
this.LogoutUser = this.LogoutUser.bind(this);
}
LogUser(User, ID){
cookies.set('User', User, { path: '/' });
cookies.set('UserID', ID,{ path: '/'});
this.setState({
currentUserId: ID,
currentUser: User
});
}
LogoutUser(){
cookies.remove('User');
this.setState({
currentUserId: null,
currentUser: null
});
}
您的渲染将变成:
render() {
return (
<div>
<div>
<Menu render={(props) => <Menu {...props} User={this.state.currentUser} ID={this.state.currentUserId} LogOutUser={this.LogoutUser} />}/>
</div>
<Router history = {history} >
<div>
//I have removed all other routes as they are not needed, but here is an example, in which the passing of props works
<Route path = "/Profile" render={(props) => <Profile {...props} User={this.state.currentUser} ID={this.state.currentUserId} LogOutUser={this.LogoutUser} />}/>
</div>
</Router>
</div>
);
}
哪个是你的菜单组件?
export default class Menu extends React.Component {
constructor(props) {
super(props);
this.toggle = this.toggle.bind(this);
this.state = {
isOpen: false,
Title: '',
};
}
toggle() {
this.setState({
isOpen: !this.state.isOpen
});
}
//here I tried to put something similar to the ComponentDidMount() in Profile.jsx, but it didn't work.
componentDidMount(){
if (this.props.User !== undefined)
{
this.setState({LoggedUser: this.props.User, UserID: this.props.ID})
this.setState({Title: "LOGGED IN"})
}
else
{
this.setState({Title: "LOGIN/REGISTER"})
}
}
render() {
console.log(this.state.User)
console.log(this.state.ID)
return (
<div>
<Navbar color="light" light expand="md">
<NavbarBrand href="/"><img src={require('./images/home.png')} width = "25px" height = "25px"/></NavbarBrand>
<NavbarToggler onClick={this.toggle} />
<Collapse isOpen={this.state.isOpen} navbar>
<Nav className="ml-auto1" navbar>
<NavItem>
<NavLink href="/Ads"><b>ADS</b></NavLink>
</NavItem>
<NavItem>
<NavLink href="/Profile"><b>YOUR PROFILE</b></NavLink>
</NavItem>
<NavItem>
//What I want to update
<NavLink href="/Login"><b>{this.state.Title}</b></NavLink>
</NavItem>
</Nav>
</Collapse>
</Navbar>
</div>
);
}
}
constructor(){
super();
this.state ={
currentUserId: cookies.get('UserID'),
currentUser: cookies.get('User')
};
this.LogUser = this.LogUser.bind(this);
this.LogoutUser = this.LogoutUser.bind(this);
}
LogUser(User, ID){
cookies.set('User', User, { path: '/' });
cookies.set('UserID', ID,{ path: '/'});
this.setState({
currentUserId: ID,
currentUser: User
});
}
LogoutUser(){
cookies.remove('User');
this.setState({
currentUserId: null,
currentUser: null
});
}
render() {
return (
<div>
<div>
<Menu render={(props) => <Menu {...props} User={this.state.currentUser} ID={this.state.currentUserId} LogOutUser={this.LogoutUser} />}/>
</div>
<Router history = {history} >
<div>
//I have removed all other routes as they are not needed, but here is an example, in which the passing of props works
<Route path = "/Profile" render={(props) => <Profile {...props} User={this.state.currentUser} ID={this.state.currentUserId} LogOutUser={this.LogoutUser} />}/>
</div>
</Router>
</div>
);
}