I have found a bunch of questions that seem like duplicates but I cannot seem to fix this issue I am having. I have a parent component:
class Parent extends Component {
constructor(props) {
super(props);
this.state = {
//some other props
currentDate: moment()
};
componentDidMount() {
this.fetchData();
}
fetchData() {
this.setState({
//set some state here
});
//Some Api call
}
onDateChange(currentDate) {
this.setState({
currentDate
});
}
//doing some stuff to render a table
return (
<div>
<ChildComponent
currentDate={this.state.currentDate}
onDateChange={(date) => this.onDateChange(date)}
/>
{currentView}
</div>
);
}
render() {
return (
<div>
//renders table
</div>
);
I am able to pass the method onDateChange to the child. And the child looks like:
class Child extends Component {
constructor(props) {
super(props);
this.state = {
currentDate: this.props.currentDate,
focused: false
};
render() {
return (
<div className="form-group">
<SingleDatePicker
date={this.state.currentDate}
onDateChange={(date) => this.props.onDateChange(date)}
focused={this.state.focused}
onFocusChange={({ focused }) => this.setState({ focused: focused })}
/>
</div>
);
For whatever reason I am unable to invoke the onDateChange method. If I console.log(this.props) I can see that the function is being passed as a prop. If I console.log(this.props.onDateChange) it returns:
ƒ onDateChange(currentDate) {
this.setState({
currentDate: currentDate
});
}
I can see the function and the code its supposed to execute. I have tried creating a method that calls this.props.onDateChange and then calling that method but it still does not work. I am confused as to why I am able to get the method to pass as a prop but I cannot invoke it. I realize there are a lot of questions asking how to pass function as a prop (I am able to do this) I just can't get this function to execute. Thanks in advance.
EDIT: I have updated the code to add the suggestions below. I am now getting no errors but It is still not working as expected. I will report back if/when i figure it out.