admin管理员组文章数量:1332339
I try this code and get an infinite loop
request.jsx
import { useEffect, useState } from 'react';
import axios from 'axios';
export async function getCategories() {
const [categories,setCategories]= useState();
try {
await useEffect(() => {
axios.get("http://localhost:3001/api/req/categories",).then(function (res) {
const response = JSON.stringify(res.data)
setCategories(response);
});
}, [])
return categorias;
} catch (error) {
console.log(error);
}
}
item_new.jsx
import { getCategories } from ".request";
import { useEffect, useState } from "react";
const [categorieSelect, setCategorieSelect] = useState();
getCategories().then(response => {
try {
const res = JSON.parse(response);
setCategorieSelect(res)
} catch (error) {
console.log(error)
}
console.log(categorieSelect) // infinite loop
})
I also try to put a useEffect and get this error
react-dom.development.js:15408 Uncaught (in promise) Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
- You might have mismatching versions of React and the renderer (such as React DOM)
- You might be breaking the Rules of Hooks
- You might have more than one copy of React in the same app See for tips about how to debug and fix this problem.<
import { getCategories } from ".request";
import { useEffect, useState } from "react";
const [categorieSelect, setCategorieSelect] = useState();
getCategories().then(response => {
try {
useEffect(()=>{
const res = JSON.parse(response);
setCategorieSelect(res)
},[])
} catch (error) {
console.log(error)
}
console.log(categorieSelect) // infinite loop
})
I try this code and get an infinite loop
request.jsx
import { useEffect, useState } from 'react';
import axios from 'axios';
export async function getCategories() {
const [categories,setCategories]= useState();
try {
await useEffect(() => {
axios.get("http://localhost:3001/api/req/categories",).then(function (res) {
const response = JSON.stringify(res.data)
setCategories(response);
});
}, [])
return categorias;
} catch (error) {
console.log(error);
}
}
item_new.jsx
import { getCategories } from ".request";
import { useEffect, useState } from "react";
const [categorieSelect, setCategorieSelect] = useState();
getCategories().then(response => {
try {
const res = JSON.parse(response);
setCategorieSelect(res)
} catch (error) {
console.log(error)
}
console.log(categorieSelect) // infinite loop
})
I also try to put a useEffect and get this error
react-dom.development.js:15408 Uncaught (in promise) Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
- You might have mismatching versions of React and the renderer (such as React DOM)
- You might be breaking the Rules of Hooks
- You might have more than one copy of React in the same app See https://reactjs./link/invalid-hook-call for tips about how to debug and fix this problem.<
import { getCategories } from ".request";
import { useEffect, useState } from "react";
const [categorieSelect, setCategorieSelect] = useState();
getCategories().then(response => {
try {
useEffect(()=>{
const res = JSON.parse(response);
setCategorieSelect(res)
},[])
} catch (error) {
console.log(error)
}
console.log(categorieSelect) // infinite loop
})
Share
Improve this question
asked Nov 20, 2024 at 19:47
SodroSodro
11 silver badge
1 Answer
Reset to default 2You've drastically over-complicated this. I don't know what examples you're following, but it looks like you're trying to use every possible operation you know of in every function you write. Don't.
If getCategories()
makes an AJAX request and returns a response then just do that. There's no reason to involve anything about React in this. For example:
export async function getCategories() {
const res = await axios.get("http://localhost:3001/api/req/categories");
return res.data;
}
Then use it in your React component to fetch data and assign that data to state:
const [categorieSelect, setCategorieSelect] = useState();
useEffect(() => {
getCategories().then(cat => setCategorieSelect(cat));
}, []);
This useEffect
will execute once when the component first loads, due to its empty dependency array.
本文标签: javascriptHow to pass a axios response to a functionStack Overflow
版权声明:本文标题:javascript - How to pass a axios response to a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742333529a2455169.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论