使用React Hooks创建具有多个输入的axios帖子

时间:2019-10-07 22:23:28

标签: reactjs axios react-hooks

我正在学习如何使用React,并开始使用类,并决定转换为使用Hooks。我相信我已经正确地配置了所有内容,但是我不确定如何使用useEffect构造axios.post来处理多个用户输入。

import React, { useState, useEffect } from 'react'
import axios from 'axios'

const Signup = () => {
    const [customerSignUp, setCustomerSignUp] = useState([
        { email: '', password: '', firstName: '', lastName: ''}
    ]);

    const handleChange = (event) => {
        setCustomerSignUp(event.target.value)
    }

    const handleSubmit = (e) => {
        e.preventDefault()
        console.log(e)
    }

    useEffect(() => {
        axios.post('/api/Customer/SignUp', {

        })
        .then(function (response) {
            console.log(response)
        })
        .catch(function (error) {
            console.log(error)
        }) 

    }, [])

我只包含了lastName,以显示我如何使用handleChange事件处理程序来更改客户的状态。

    return (
        <div className="container">
            <form className='white' onSubmit={handleSubmit}>
                <h5 className="grey-text.text-darken-3">Sign Up With Email</h5>                        
                <div className="input-field">
                    <label htmlFor="lastName">Last Name</label>
                    <input type="text" name="lastName" value={customerSignUp.lastName} onChange={handleChange} required />
                </div>
                <div className="input-field"> 
                    <button className="btn blue darken-3" type="submit">Sign Up</button>
                </div>
            </form>
        </div>
    );
}
export default Signup

1 个答案:

答案 0 :(得分:2)

您的axios请求不必在useEffect()内部,实际上,通过您的注册功能,最好将其保留在handleSubmit函数中。 您可以选择将useEffect()设置为在首次进行组件渲染之后或对特定依赖项进行每次更改之后触发一次。两者都不适合您的功能。

此外,让状态保存一个对象而不是对象数组似乎更有意义。从那里,您可以像下面这样将状态放入axios请求中:

import React, { useState, useEffect } from 'react'
import axios from 'axios'

const Signup = () => {
    const [customerSignUp, setCustomerSignUp] = useState(
        { email: '', password: '', firstName: '', lastName: ''}
    );

    const handleChange = (event) => {
        setCustomerSignUp({...customerSignup, [event.target.name]: event.target.value})
    }

    const handleSubmit = (e) => {
        e.preventDefault()
        axios.post('/api/Customer/SignUp', customerSignup)
          .then(function (response) {
              console.log(response)
          })
          .catch(function (error) {
              console.log(error)
          }) 
    }

还请记住为与状态中的字段相对应的每个输入赋予名称属性。 (看起来您已经拥有了)

    return (
        <div className="container">
            <form className='white' onSubmit={handleSubmit}>
                <h5 className="grey-text.text-darken-3">Sign Up With Email</h5>                        
                <div className="input-field">
                    <label htmlFor="lastName">Last Name</label>
                    <input type="text" name="lastName" value={customerSignUp.lastName} onChange={handleChange} required />
                </div>
                <div className="input-field"> 
                    <button className="btn blue darken-3" type="submit">Sign Up</button>
                </div>
            </form>
        </div>
    );
}
export default Signup