如何在React中以可重用形式传递道具

时间:2019-01-15 14:58:29

标签: javascript reactjs forms

我正在React中构建一个可重用的表单,以用作may应用程序中的登录和登录表单。 我正在使用Axios将数据发送到数据库。

但是我无法将输入值作为道具发送到我的UserSignIn组件,以在Axios的函数中使用它们。这是代码:
我如何获得这些道具?谢谢

AccountForm组件:

import React, { Component } from "react";
import {Grid,Input, InputLabel, FormControl, Typography, Button, Paper,  
 } from "@material-ui/core";

class AccountForm extends Component {
constructor(props) {
  super(props);
  this.state = {
   userSignUpName: "",
   userSignUpEmail: ""
}

  handleChange(e) {
  this.setState({
  [e.target.id]: e.target.value
});
}

render() {
  const {
  classes,
  formType,
  onCreateAccount,
  onSubmit,
  onBlurCheck,
  message,
  title
 } = this.props;

 const {
  userSignUpName,
  userSignUpEmail,
 } = this.state;

  return (
  <div>
    <Grid container justify="center">
      <Grid item xs={10}>
        <Paper>
          <form
            onChange={e => this.handleChange(e)}
            onSubmit={onSubmit}
          >
            <Typography variant="subheading" color="primary"   
 align="center">
              {title}
            </Typography>
            {formType === "signin" && (
              <FormControl>
                <InputLabel htmlFor="name">Nom</InputLabel>
                <Input id="userSignUpName" name="name" type="text" />
              </FormControl>
            )}
            <FormControl>
              <InputLabel htmlFor="email">Email</InputLabel>
              <Input id="userSignUpEmail" type="email" name="email" />
                 </FormControl>
              </form>
            </Paper>
          </Grid>
        </Grid>
      </div>
    );
  }
 }

 export default AccountForm;

UserSignIn组件:

import React, { Component } from "react";
import axios from "axios";
import config from "../../assets/lib/axiosConfig";
import { AccountForm } from "./Index";


 class UserSignIn extends Component {
  constructor(props) {
  super(props);
 this.state = {
  formType: "signin",
  title: "Create account"
  };
}


 handleSubmit(e) {
  e.preventDefault();
  axios(
   config(
    {
      name: this.props.userSignUpName,
      email: this.props.userSignUpEmail,

    },
    "/createAccount",
    "post"
   )
  ).then(res => {
   const { success, error, token } = res.data;
   if (success) {
    localStorage.setItem("AuthToken", token);
    this.props.redirectTo();
   }
   if (error) {
     this.setState({ message: error });
    }
  });
}

 render() {
 const { prevPath, history, userSignUpName } = this.props;
 // I can't get userSignUpName props
 const { formType, message, title } = this.state;
 return (
  <AccountForm
    {...this.props}
    title={title}
    onSubmit={e => this.handleSubmit(e)}
    formType={formType}
    redirectTo={
      prevPath !== null && prevPath === "/mycart"
        ? () => history.push("/payment")
        : () => history.push("/")
      }
     />
    );
  }
}

export default UserSignIn;

谢谢

1 个答案:

答案 0 :(得分:0)

您可以将表单数据作为handleSubmit参数传递

AccountForm组件:

    <form
        onChange={e => this.handleChange(e)}
        onSubmit={(e) => {
                e.preventDefault()
                onSubmit({
                    userSignUpName: this.state.userSignUpName,
                    userSignUpEmail: this.state.userSignUpEmail,
                })
            }}
      >

UserSignIn组件:

handleSubmit(params) {
  axios(
   config(
    {
      name: params.userSignUpName,
      email: params.userSignUpEmail,

    },
   //....
}