I have a parent component (App) that holds the state
. Inside this component I have some code that manipulates the state through event handlers. So far so good.
However, I need to display the current state
inside a child component (ChildComponent). I tried to achieve this by passing the state as a property to the ChildComponent, however I lose the dynamic coupling to the state. Any change to state is not reflected in the child component.
class ChildComponent extends React.Component {
constructor(props) {
super(props);
}
render () {
// This should get dynamically updated
return <div>{this.props.value.toString()}</div>
}
}
class App extends React.Component {
constructor(props) {
super(props);
this.state = {value: 1};
}
// Do stuff here that changes state
render() {
return <ChildComponent value={this.state.value} />
}
}
works now: Updated correct example on codepen
This example is a basic representation of my problem. I think I followed the example of the official docs, and specifically lifting state up. Somehow it does not work though. Could be me misinterpreting the docs (those also happen to be not so good).
edit: maybe having to do with the child component being a class rather than a function?