I’m fairly new to React.There is a problem when I map my child component , DatePicker shows same results in onChange function. Thanks for your help in advance
class App extends Component {
constructor(props) {
super(props);
this.state = {
startDate: new Date(),
options: [1,2,3,4,5,6,7,8,9,10],
countValue: '',
showMessage:false,
persons:[],
}
this.someMethod=this.someMethod.bind(this)
};
someMethod(date) {
this.setState({
startDate: date
});
console.log('bar');
}
PersonsCount (event) {
let arr = [];
for (let i=1;i<=event.target.value;i++) {
arr.push(
{id:i,birthDate:'',skiPrice: 0, rentDays: 0, sale: 0}
)
}
this.setState({
persons: arr , showMessage: !this.state.showMessage, countValue: event.target.value
})
};
render() {
const {options,showMessage} =this.state;
return (
<div>
<span>Number of people : </span>
<select onChange={this.PersonsCount.bind(this)}>
{
options.map(item => {
return (<option value={item} key={item}>{item}</option>)
})
}
</select>
{
(showMessage) ? <p>Choose birthday date</p> : null
}
<ol>
{
this.state.persons
.map((item, index) =>
<li key={item.id}>
<Age value={this.state.startDate} parentMethod={this.someMethod} key={item.id}
index={index} >{this.props.children}</Age>
</li>
)
}
</ol>
</div>
)
}
}
class Age extends Component {
constructor(props) {
super(props);
};
click = (date) => {
this.props.parentMethod(date);
};
render() {
return (
<DatePicker
selected={this.props.value}
onChange={this.click}
peekNextMonth
showMonthDropdown
showYearDropdowncd
dropdownMode="select"
/>
)
}
}