0

我正在尝试按照react-phone-number-input以下方式集成一个名为我的多步反应表单的库:

import React, {useState} from 'react'
import Helmet from 'react-helmet'
import './reg-style/Credentail.css'
import 'react-phone-number-input/style.css'
import PhoneInput from 'react-phone-number-input'

export default function Credential (){
    continue = e => {
        e.preventDefault();
        this.props.nextStep();
      };

      back = e => {
        e.preventDefault();
        this.props.prevStep();
      };

          const [ values, handleChange ] = this.props;
          const [value, setValue] = useState()
        return (
            <div>
                  <Helmet>
                  <body className="form-bg-col"></body>
                </Helmet>


                <div className="login">
                  <div className="reg-container">
                  <button onClick={this.back} className="col-form-prev"><i class="fa fa-chevron-left"></i></button>
                  <div className="user-detail email">
                      Email:
                      <input onChange={handleChange('Email')} defaultValue={values.Email}  type="email" placeholder="Enter your Email"/>
                  </div>
                  <div  className="user-detail" 
                  onChange={handleChange('Phone')} defaultValue={values.Phone}
                  >
                      Phone:
                      <PhoneInput
                        displayInitialValueAsLocalNumber
                        placeholder="Enter phone number"
                        defaultCountry="IN"
                        value={value}
                        onChange={setValue}
                        />

                  </div>
                  <div className="user-detail username">
                      Username:
                      <input onChange={handleChange('Username')} defaultValue={values.Username} type="text" placeholder="Select your unique Username" />
                  </div>
                  <div className="user-detail password">
                      Password:
                      <input onChange={handleChange('Password')} defaultValue={values.Password} type="password" placeholder="Choose your Password" />
                  </div>

                  <button onClick={this.continue} className="col-form-next"><i class="fa fa-chevron-right"></i></button>


                  </div>

                </div>
            </div>
        )

}

在运行它时,我收到一个错误:

 `./src/form/register/Credential.js
  Line 9:14:  Parsing error: Unexpected token

   7 | 
   8 | export default function Credential (){
>  9 |     continue = e => {
     |              ^
  10 |         e.preventDefault();
  11 |         this.props.nextStep();
  12 |       };`

我应该怎么做才能摆脱这个错误,你的帮助对我来说非常有价值

4

1 回答 1

1

continue是 javascript 中的保留关键字。

因此,只需为您的方法使用不同的名称。


另一个问题是您似乎正在使用在类中声明方法的语法。由于您现在使用的是 a function,因此您必须声明您的变量,并且在使用它们时您不应该使用它们,this.backback因为它不是对象的一部分,只是一个局部变量。

import React, { useState } from "react";
import Helmet from "react-helmet";
import "./reg-style/Credentail.css";
import "react-phone-number-input/style.css";
import PhoneInput from "react-phone-number-input";

export default function Credential() {
  const alter = e => {
    e.preventDefault();
    this.props.nextStep();
  };

  const back = e => {
    e.preventDefault();
    this.props.prevStep();
  };

  const [values, handleChange] = this.props;
  const [value, setValue] = useState();
  return (
    <div>
      <Helmet>
        <body className="form-bg-col" />
      </Helmet>

      <div className="login">
        <div className="reg-container">
          <button onClick={back} className="col-form-prev">
            <i class="fa fa-chevron-left" />
          </button>
          <div className="user-detail email">
            Email:
            <input
              onChange={handleChange("Email")}
              defaultValue={values.Email}
              type="email"
              placeholder="Enter your Email"
            />
          </div>
          <div
            className="user-detail"
            onChange={handleChange("Phone")}
            defaultValue={values.Phone}
          >
            Phone:
            <PhoneInput
              displayInitialValueAsLocalNumber
              placeholder="Enter phone number"
              defaultCountry="IN"
              value={value}
              onChange={setValue}
            />
          </div>
          <div className="user-detail username">
            Username:
            <input
              onChange={handleChange("Username")}
              defaultValue={values.Username}
              type="text"
              placeholder="Select your unique Username"
            />
          </div>
          <div className="user-detail password">
            Password:
            <input
              onChange={handleChange("Password")}
              defaultValue={values.Password}
              type="password"
              placeholder="Choose your Password"
            />
          </div>

          <button onClick={alter} className="col-form-next">
            <i class="fa fa-chevron-right" />
          </button>
        </div>
      </div>
    </div>
  );
}

于 2020-03-24T19:30:40.897 回答