gpt4 book ai didi

reactjs - 如何使用带有 React Hooks 的 axios 发布请求?

转载 作者:行者123 更新时间:2023-12-02 16:21:07 26 4
gpt4 key购买 nike

这是我的注册组件

const SignupComponent = () => {
const [values, setValues] = useState({
username: 'silvio1',
name: 'Silvioo',
email: 'berlusconi@gmail.com',
password: '123ooo007',
});
const [loading, setLoading] = useState(false);

const handleSubmit = async (e) => {
e.preventDefault();

const { username, name, email, password } = values;
const user = {username, name, email, password};

await axios.post('${API)/signup', user);
};

const handleChange = name => e => {
setValues({ ...values, [name]: e.target.value });
};

const showLoading = () => (loading ? <div className="alert alert-info">Loading...</div> : '');

const signupForm = () => {
return (
<form onSubmit={handleSubmit}>
<div className="form-group">
<input
value={values.username}
onChange={handleChange('username')}
type="text"
className="form-control"
placeholder="Type your username"
/>
</div>

<div className="form-group">
<input
value={values.name}
onChange={handleChange('name')}
type="text"
className="form-control"
placeholder="Type your name"
/>
</div>

<div className="form-group">
<input
value={values.email}
onChange={handleChange('email')}
type="email"
className="form-control"
placeholder="Type your email"
/>
</div>

<div className="form-group">
<input
value={values.password}
onChange={handleChange('password')}
type="password"
className="form-control"
placeholder="Type your password"
/>
</div>

<div>
<button className="btn btn-primary">Signup</button>
</div>
</form>
);
};

return <React.Fragment>
{showLoading()}
{signupForm()}
</React.Fragment>;
};

export default SignupComponent;

编辑我更改了我的代码(zhulien 接受的答案)。出现注册页面,我尝试注册用户。我有错误

Unhandled Runtime Error
Error: Request failed with status code 404

Call Stack
createError
node_modules/axios/lib/core/createError.js (16:0)
settle
node_modules/axios/lib/core/settle.js (17:0)
XMLHttpRequest.handleLoad
node_modules/axios/lib/adapters/xhr.js (62:0)

前端文件夹

components
config.js
next.config.js
node_modules
package.json
package-lock.json
pages

我的页面文件夹

_document.js
index.js
signin.js
signup.js

signup.js导入上面的代码

import Link from 'next/link';
import Layout from '../components/Layout';
import SignupComponent from '../components/frontauth/SignupComponent';

const Signup = () => {
return (
<Layout>
<h2>Signup page</h2>
<SignupComponent />
</Layout>
);
};

我的 next.config.js

{
APP_NAME: 'BLOG FRONTEND',
APP_DEVELOPMENT: 'http://localhost:3000',
PRODUCTION: false
}

和config.js

const { publicRuntimeConfig } = getConfig();

console.log(publicRuntimeConfig);
export const API = publicRuntimeConfig.PRODUCTION
? 'https://cryptoblog.com'
: 'http://localhost:3000';
export const APP_NAME = publicRuntimeConfig.APP_NAME;

我是 React 和 React Hooks 的新手。如何解决这个问题?

最佳答案

首先,您尝试访问 {username}(不存在)而不是 values.username 状态属性。此外,不要在事件处理程序中使用 Hook ,它们应该在组件的顶层主体或仅在自定义 Hook 中使用。检查这个:React hooks rules .

所以:

  1. 在您的表单中,您必须使用 state(values) 属性。
  2. 在组件的主体流程中提取 useEffect Hook ,或者最好将其完全删除,因为您目前没有正确使用它。你最好只使用简单的表单提交事件处理程序,它应该在某处发布数据而不设置任何状态。

您的代码可能类似于:

import axios from 'axios';
import React, { useEffect, useState } from 'react';
import { API } from '../../config';

const SignupComponent = () => {
const [values, setValues] = useState({
username: 'silvio1',
name: 'Silvioo',
email: 'berlusconi@gmail.com',
password: '123ooo007',
});

const [loading, setLoading] = useState(false);

const handleSubmit = async (e) => {
e.preventDefault();

const { username, name, email, password } = values;
const user = {username, name, email, password};

await axios.post('${API)/signup', user);
};

const handleChange = name => e => {
setValues({ ...values, [name]: e.target.value });
};

const showLoading = () => (loading ? <div className="alert alert-info">Loading...</div> : '');

const signupForm = () => {
return (
<form onSubmit={handleSubmit}>
<div className="form-group">
<input
value={values.username}
onChange={handleChange('username')}
type="text"
className="form-control"
placeholder="Type your username"
/>
</div>

关于reactjs - 如何使用带有 React Hooks 的 axios 发布请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65456583/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com