从子组件访问 this.state 个值

Access this.state values from child component

我无法将日期值从 react-dates 组件传递到它的父组件。

首先,我通常会使用 Redux 来处理这样的任务,但这对项目来说太过分了,我更喜欢一个简单的解决方案。

如何将 onChange 事件从 DateRangePicker 传递到它的父组件?

父组件

...    
import DateRangePicker from './DatepickerRates'

class Rates extends Component {
    constructor(props) {
        super(props);
        this.state = { }
    }

    onChange(startDate, endDate) {
        this.setState({[startDate]: startDate});
        this.setState({[endDate]: endDate});
    }

    render() {
        console.log(`In ${this.state.startDate} - Out ${this.state.endDate}`)

        return (
            <Wrapper>
              <DateRangePicker onChange={this.onChange.bind(this)}/>
            </Wrapper>
        );
    }
}
export default Rates;

DateRangePicker

...
class DateRangePickerWrapper extends Component {
    constructor(props) {
        super(props);
        this.state = {
            focusedInput,
            startDate: props.initialStartDate,
            endDate: props.initialEndDate,
        }
        this.onDatesChange = this.onDatesChange.bind(this);
        this.onFocusChange = this.onFocusChange.bind(this);
    }

    onDatesChange({ startDate, endDate }) {
        const { stateDateWrapper } = this.props;
        this.props.onChange('startDate', this.state.startDate);
        this.props.onChange('endDate', this.state.endDate);
        this.setState({
          startDate: startDate && stateDateWrapper(startDate),
          endDate: endDate && stateDateWrapper(endDate),
        });
    }

    render() {
        const { startDate, endDate } = this.state;

        ...

        return (
            <div>
                <DateRangePicker
                    {...props}
                    onDatesChange={this.onDatesChange}
                    onFocusChange={this.onFocusChange}
                    focusedInput={focusedInput}
                    startDate={startDate}
                    endDate={endDate}
                />
            </div>
        );
    }
}

export default DateRangePickerWrapper;

像这样将 onChange 函数作为 props 传递给子组件

import DateRangePicker from './DatepickerRates'
class Rates extends Component {
constructor(props) {
    super(props);
    this.state = {
        startDate: '',
        endDate: '',
    }

}

onChange = (startDate, endDate) => {
    this.setState({
        startDate: startDate,
        endDate: endDate
    });
}

render() {
    ...
    return (
        <Wrapper>
          <DateRangePicker
            onChange={this.onChange}/>
        </Wrapper>
    );
}
}
export default Rates;

然后从子组件将您的 onDatesChange 函数修改为

   onDatesChange({ startDate, endDate }) {
    const { stateDateWrapper } = this.props;
    console.log('startDate: ', startDate)
    console.log('endDate: ', endDate)
    this.props.onChange(startDate, endDate);
    this.setState({
      startDate: startDate && stateDateWrapper(startDate),
      endDate: endDate && stateDateWrapper(endDate),
    });
}

注意我添加了日志来检查您是否收到了开始和结束日期值

这是工作演示 - https://codesandbox.io/s/vqw83my5l0 在你的父组件上设置一个像 onChange={this.handleDropDownChange} 并在子组件 onChange of date 中,在 onDatesChange 和函数中设置 this.props.onChange(startDate, endDate) 并且您将在 handleDropDownChange()

中获取数据