I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.
You’ve written the function getDayMonth, but you’re not using it. When you render your childClass component, you’re using code that hasn’t been defined:
You should be using this.props.currentMonth. You’ve already called the function and it’s returned a value, so trying to call it again results in undefined.
Well, I can’t see all of your code, so I don’t know where the problem is. You should simplify things to make debugging easier, though. First, get rid of the function getDayMonth. Placing the current date in your component’s state makes more sense. I’ll assume you’re using ES6 classes because that’s what everyone’s doing these days.
class Parent extends Component {
constructor() {
super()
this.state = {
currentMonth: new Date().getMonth(),
currentDate: new Date().getDate()
}
}
render() {
return (
<childClass
currentMonth={this.state.currentMonth}
currentDate={this.state.currentDate}
/>
)
}
}
Then your childClass component should work as you’ve written it. If this doesn’t work, then you’ve got bigger problems than passing values.
Hi,
I tried to add the values in State and when I tried printing the currentDate in console from the child component, I am getting error as currentDate not defined…