简体   繁体   English

如何使用props在React中自动填充可编辑的redux-form字段?

[英]How can I use props to auto-populate editable redux-form fields in React?

I'm new to React so I've tried to show as much code as possible here to hopefully figure this out! 我是React的新手,所以我试图尽可能多地显示代码,希望能够解决这个问题! Basically I just want to fill form fields with properties from an object that I fetched from another API. 基本上我只想填写表单字段,其中包含我从另一个API获取的对象的属性。 The object is stored in the autoFill reducer. 该对象存储在autoFill reducer中。 For example, I would like to fill an input with autoFill.volumeInfo.title, where the user can change the value before submitting if they want. 例如,我想用autoFill.volumeInfo.title填充输入,用户可以在提交之前更改值,如果需要的话。

I used mapDispatchtoProps from the autoFill action creator, but this.props.autoFill is still appearing as undefined in the FillForm component. 我使用了autoFill动作创建器中的mapDispatchtoProps,但是this.props.autoFill仍然在FillForm组件中显示为undefined。 I'm also confused about how to then use props again to submit the form. 我也对如何再次使用道具提交表单感到困惑。 Thanks! 谢谢!

My reducer: 我的减速机:

import { AUTO_FILL } from '../actions/index';

export default function(state = null, action) {
  switch(action.type) {
  case AUTO_FILL:
      return action.payload;
  }

  return state;
}

Action creator: 行动创造者:

export const AUTO_FILL = 'AUTO_FILL';

export function autoFill(data) {

  return {
    type: AUTO_FILL,
    payload: data
  }
}

Calling the autoFill action creator: 调用autoFill动作创建者:

class SelectBook extends Component {
   render() {

     return (

      ....

     <button
       className="btn btn-primary"
       onClick={() => this.props.autoFill(this.props.result)}>
       Next
     </button>
    );
   }
}

....

function mapDispatchToProps(dispatch) {
  return bindActionCreators({ autoFill }, dispatch);
}

export default connect(mapStateToProps, mapDispatchToProps)(SelectBook);

And here is the actual Form where the issues lie: 以下是问题所在的实际表格:

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { reduxForm } from 'redux-form';
import { createBook } from '../actions/index;

class FillForm extends Component {

  constructor(props) {
    super(props);
    this.state = {value: ''};

    this.handleChange = this.handleChange.bind(this);
  }

  onSubmit(props) {
   this.props.createBook(props)
  }

  handleChange(event) {
    this.setState({value: event.target.value});
  }

  render() { 

    const { fields: { title }, handleSubmit } = this.props;

    return (

       <form {...initialValues} onSubmit={handleSubmit(this.onSubmit.bind(this))}>
          <input type="text" className="form-control" name="title" {...title} />
          <button type="submit">Submit</button>
       </form>
      )
   }

  export default reduxForm({
    form: 'AutoForm',
    fields: ['title']
  },
  state => ({
    initialValues: {
      title: state.autoFill.volumeInfo.title
    }
  }), {createBook})(FillForm)

I think you're mixing up connect and reduxForm decorators in the actual form component. 我认为你在实际的表单组件中混合了connectreduxForm装饰器。 Currently your code looks like this (annotations added by me): 目前您的代码看起来像这样(我添加的注释):

export default reduxForm({
  // redux form options
  form: 'AutoForm',
  fields: ['title']
},

// is this supposed to be mapStateToProps?
state => ({
  initialValues: {
    title: state.autoFill.volumeInfo.title
  }
}),
/// and is this mapDispatchToProps?
{createBook})(FillForm)

If this is the case, then the fix should be as simple as using the connect decorator as it should be (I also recommend separating this connect props to their own variables to minimize confusions like this): 如果是这种情况,那么修复应该像使用connect装饰器一样简单(我还建议将这个连接道具分离到它们自己的变量,以尽量减少这种混淆):

const mapStateToProps = state => ({
  initialValues: {
    title: state.autoFill.volumeInfo.title
  }
})

const mapDispatchToProps = { createBook }

export default connect(mapStateToProps, mapDispatchToProps)(
  reduxForm({ form: 'AutoForm', fields: ['title'] })(FillForm)
)

Hope this helps! 希望这可以帮助!

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

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