I am new to React so sorry if this is a bad question. I am having trouble with changing the state of a child component then calling a method that changes the state of the parent component. Right now when I trigger the method that is supposed to change states the parent state changes as it should but the child state does not change at all.
When I remove the state change on the Parent component the child component changes as it should.
Here is the parent component (edited to remove code not related to the problem)
class Parent extends React.Component {
constructor(props) {
super(props);
this.state = {
loginModalName: "Log in"
}
this.bindedUpdateModalName = this.updateModalName.bind(this)
}
updateModalName(name) {
console.log(name);
this.setState({
loginModalName: name
});
};
render() {
return (
<Child loginSwitch = {this.bindedUpdateModalName}/>
);
}
}
and here is the child component
class Child extends React.Component {
constructor(props) {
super(props);
this.state = {
isLoggingIn: true
}
}
toggleLoggingIn = () => {
this.setState({
isLoggingIn: !this.state.isLoggingIn
});
console.log(this.state.isLoggingIn);
this.props.loginSwitch(this.state.isLoggingIn ? "Log in" : "Register");
};
render() {
return (
<Button onClick={this.toggleLoggingIn} type="link" style={{paddingLeft: 0}}>register now!</Button>
);
}
There are no error messages, but my logs show
true
Log In
every time I click the button