reactjs - 使用 React Hooks 创建具有多个输入的 axios post

标签 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

最佳答案

您的 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)
          }) 

setCustomerSignUp({ email: '', password: '', firstName: '', lastName: '' });

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

    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

关于reactjs - 使用 React Hooks 创建具有多个输入的 axios post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58277874/

相关文章:

javascript - 带有 Mono Repo 的 react-scripts 的 testPattern --config=jest.config.js 无效

axios - 如何在 Promise 中使用 Babel Polyfill?

arrays - 使用 Vuejs 和 Axios 合并对象

reactjs - React Function组件使用局部变量设置状态变量V/s

reactjs - 使用输入类型文本对 useState 和日期使用react

node.js - 构建Reactjs空白页面并且不生成其他页面

javascript - Material UI 自动完成,redux-form 值被破坏

reactjs - SPA + 返回按钮 + 表单状态

javascript - Api请求在swagger和postman中工作但在js中不起作用(使用axios)

javascript - React Hook "useSWR"被有条件地调用。 React Hooks 必须在每个组件渲染中以完全相同的顺序调用