The problem is that you are trying to access the state and methods of the Minuteur component from the parent component App.
const App = () => {
return (
<div>
<TimerInput value={this.state.seconds} handleChange={this.handleChange} />
<Minuteur seconds={600} libelle="Pâtes"/>
</div>
)
}
this.state.seconds and this.handleChange refer to attributes on the Minuteur component. Since it seems like TimerInput and Minuteur need to share some state, you have a couple options.
- Combine the
TimerInput and Minuteur components into one so they have the same state.
- Create a wrapping component that contains both
TimerInput and Minuteur and move the shared state (e.g. state.seconds) to that component.
The approach for option 2 would look roughly like this:
class Wrapper extends React.Component {
constructor(props) {
super(props)
this.state = {
seconds: 0
}
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
this.setState({
seconds: event.target.value
})
}
render() {
return (
<div>
<TimerInput value={this.state.seconds} handleChange={this.handleChange} />
<Minuteur seconds={this.state.seconds} libelle="Pâtes"/>
</div>
)
}
const App = () => {
return (
<div>
<Wrapper />
</div>
)
}
secondsfield of something before it has been defined. There's nothing there yet. This problem arises a lot. (BTW, this is a problem that cannot arise in some static typed languages, such as Haskell, because they make it impossible to create a thing that has no value.)