ReactJS: не удается привести данные в состояние с помощью многошаговых форм - PullRequest
0 голосов
/ 21 мая 2019

Я работаю с многошаговыми формами, поэтому я создал многошаговые формы в разных functions за пределами class. Из-за этого поля ввода значения получают как undefined в states. Я использую FormData, чтобы получить входные значения. Вот пример кода.

class AccountInfo extends Component {
  constructor(props) {
    super(props) 
    this.state = {
      currentStep: 1,
      userAccountData: {
        userid: '',
        imgearray: [],
        name: '',
      }
    }
    this.handleSubmit = this.handleSubmit.bind(this);
  }

handleFileUpload = (event) => {
    this.setState({imgearray: event.currentTarget.files[0]})
  };

handleSubmit = event => {
    let that = this;
    const { AccountAction, userID } = that.props;

    event.preventDefault();

    const data = new FormData(event.target);

    let accountInputs = {
      userid: 68,
      imgearray: that.state.imgearray,
      name: data.get('name'),
}
that.setState({
      userAccountData: accountInputs,
    })
}

AccountInfoView = () => {
  return (
    <div className="container">
    <React.Fragment>
      <Formik
        initialValues={{name:'', email: '', phone: '', bio: '' }}
        validationSchema={accountInfoSchema}
        render={() => {
        return(
      <Form onSubmit={this.handleSubmit}>
      <Step1 currentStep={this.state.currentStep} file= {this.state.imgearray} handleFileUpload={this.handleFileUpload} name={this.state.userAccountData['name']}
        bio= {this.state.bio}
        phone= {this.state.phone}
        gender= {this.state.gender}
        birthdate= {this.state.birthday}/>
      <Step2 currentStep={this.state.currentStep} />
      <Step3 currentStep={this.state.currentStep} />
      </Form>
      );
    }}
    />
    </React.Fragment>
    </div>
  )
}
render() {    
  return (
    <div>{this.AccountInfoView()}</div>
  )
}

}

function Step1(props)  {

  if (props.currentStep !== 1) {
    return null
  } 
  return(
    <React.Fragment>
    <div className="inner_account">
      <h2 className="accsec_title">Create your account</h2>
      <div className="upload">
          <label htmlFor="profile">
            <div className="imgbox">
              <img src={siteurl + IMAGE.trans_116} alt="" /> 
              { props.file ? <img src={props.file && URL.createObjectURL(props.file)} className="absoImg" alt="" /> : 
                <img src={imgurl} className="absoImg" alt="" /> }
            </div>
          </label>
          <input id="profile" name="file" type="file" accept="image/*" onChange={props.handleFileUpload}/>
          <span className="guide_leb">Add your avatar</span>
        </div>
      <div className="reg_form">
        <ul>
          <li>
            <div className="custom_group">
              <Field type="text" name="name" className="trans text_input" placeholder="Enter your name" />
              <label className="label_two">Name</label>
              <ErrorMessage name="name" />
            </div>
          </li>
        </ul>
      </div>
    </React.Fragment>
  );
}
...