I have a component called AvailabiltyCalender
and it has a child component called DatePicker
. The Chile component has a state called focusedInput
. I need to access that state from the AvailabiltyCalender component and get that state value. How can i do that.
AvailabiltyCalender component
import React, { Component } from 'react';
import './AvailabilityCalender.css';
import backArrow from '../../../assets/images/back.png';
import DatePicker from '../../Common/DatePicker/DatePicker';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import PropTypes from "prop-types";
import moment from 'moment';
class AvailabiltyCalender extends Component {
componentWillReceiveProps(nextProps) {
}
render() {
const { checkInOutDates: { checkInDate, checkOutDate } } = this.props;
let checkIn = checkInDate.format('ddd DD MMM');
let checkOut = checkOutDate.format('ddd DD MMM');
return (
<div className="full_width checkin_checkout_wr">
<DatePicker />
</div>
);
}
}
AvailabiltyCalender.propTypes = {
checkInOutDates: PropTypes.object.isRequired
};
function mapStateToProps(state, ownProps) {
const dates = state.searchPageReducer.checkInOutDates;
const checkDates = {
checkInDate: moment(dates.checkInDate),
checkOutDate: moment(dates.checkOutDate)
}
return {
checkInOutDates: checkDates,
};
}
export default connect(mapStateToProps)(AvailabiltyCalender);
DatePicker Component
import React, {Component} from 'react';
import 'react-dates/initialize';
import {DateRangePicker, SingleDatePicker, DayPickerRangeController} from 'react-dates';
import 'react-dates/lib/css/_datepicker.css';
import './DatePicker.css';
import moment from "moment";
import {connect} from "react-redux";
import PropTypes from "prop-types";
import {bindActionCreators} from "redux";
import * as searchPageActions from "../../../pages/Search/searchPageActions";
class DatePicker extends Component
{
constructor(props)
{
super(props);
let check_in = moment(this.props.checkInOutDates.checkInDate);
let check_out = moment(this.props.checkInOutDates.checkOutDate);
this.state = {
startDate: check_in,
endDate: check_out,
};
this.onDatesChange = this.onDatesChange.bind(this);
}
onDatesChange({ startDate, endDate }) {
this.setState({ startDate, endDate });
this.props.actions.retrieveCalendarResults({
checkInDate: startDate,
checkOutDate: endDate,
});
// if(this.state.focusedInput === 'endDate'){
// }
}
render()
{
const { focusedInput } = this.state;
const { checkInOutDates: { checkInDate, checkOutDate} } = this.props
return (
<div className="DateRangePickerInput DateRangePickerIcon">
<DateRangePicker
startDate={checkInDate}
startDateId="checkin_date_id"
endDate={checkOutDate}
endDateId="checkout_date_id"
onDatesChange={({ startDate, endDate }) => this.onDatesChange({ startDate, endDate }) }
focusedInput={this.state.focusedInput}
onFocusChange={focusedInput => this.setState({focusedInput}) }
orientation="vertical" withFullScreenPortal
startDatePlaceholderText="Checkin"
endDatePlaceholderText="Checkout"
/>
</div>
);
}
}
DatePicker.propTypes = {
checkInOutDates: PropTypes.object.isRequired
};
function mapStateToProps(state, ownProps)
{
const dates = state.searchPageReducer.checkInOutDates;
const checkDates = {
checkInDate: moment(dates.checkInDate),
checkOutDate: moment(dates.checkOutDate)
}
return {
checkInOutDates: checkDates
};
}
function mapDispatchToProps(dispatch)
{
return {
actions: bindActionCreators(searchPageActions, dispatch)
};
}
export default connect(mapStateToProps, mapDispatchToProps)(DatePicker);