简体   繁体   English

将组件状态传递给redux-form

[英]Pass component state to redux-form

I am using react-day-picker with multi day select http://react-day-picker.js.org/examples/selected-multiple . 我在多日期选择http://react-day-picker.js.org/examples/selected-multiple中使用react-day-picker

My question: Is it possible to pass state of this component: 我的问题:是否可以传递此组件的状态:

export default class MultiDayPicker extends Component {
state = {
   selectedDays: []
};

handleDayClick = (day, {selected}) => {
   const {selectedDays} = this.state;

if (selected) {
  const selectedIndex = selectedDays.findIndex(selectedDay =>
    DateUtils.isSameDay(selectedDay, day)
  );

  selectedDays.splice(selectedIndex, 1);
} else {
  selectedDays.push(day);
}
this.setState({selectedDays});
 };

 render() {
return (
  <div>
    <DayPicker
      selectedDays={this.state.selectedDays}
      onDayClick={this.handleDayClick}
    />
  </div>
);

to redux-form state ?? redux-form状态?

<Field
    name="dates"
    component={MutliDayPicker}
/>

In your custom MultiDayPicker component you have to call redux-form input.onChange method. 在自定义的MultiDayPicker组件中,您必须调用redux-form input.onChange方法。

So in order to change the redux-form state, when your MultiDayPicker value is changed, you have to add the following redux-form api call in handleDayClick : 因此,为了更改redux-form状态,当您更改MultiDayPicker值时,您必须在handleDayClick添加以下redux-form api调用:

handleDayClick( day, { selected }) => {
  // Rest code here ...

  // Update `redux-form` state
  this.props.input.onChange(selectedDays)
}

Also you have to think about the rest redux-form input properties and callbacks and decide for which ones to add support. 另外,您还必须考虑其余的redux-form输入属性和回调,并确定要添加哪些支持。 Here's the full list . 这是完整列表

I've never used the DayPicker, but i've used DayPickerInput, if you can it instead of DayPicker, you can simply put inside DayPickerInput inputProps={{...this.props.input}} as inputProps is defined as additional props to add to the input component. 我从未使用过DayPicker,但是我使用过DayPickerInput,如果可以代替DayPicker,则可以将其放置在DayPickerInput inputProps={{...this.props.input}}因为inputProps被定义为其他道具添加到输入组件。

Redux Form need to find the input name inside of the field you pass from Field to bind the value. Redux Form需要在您从Field传递的字段中查找输入名称以绑定值。

You should also use onDayChange instead of onDayClick i guess. 我猜你也应该使用onDayChange而不是onDayClick。

Code: 码:

<DayPickerInput inputProps={{...this.props.input}} onDayChange={this.props.input.onChange} />

You can find more information here: DayPickerInput: inputProps 您可以在此处找到更多信息: DayPickerInput:inputProps

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM