为什么尽管推动了元素,但我的温度阵列是否在第二使用效果中不包含元素?
我正在尝试获取子主题数据,因此首先我必须获取用户注册的课程,然后在每门课程中,我都会获得章节,然后在每一章中,我都会得到主题,最后,我得到了每个主题的子主题并将它们全部推入温度阵列。 但是,当我尝试在第二个使用效果中打印它时,尽管在FetchSubtopics功能中打印了一个空数组,但它返回了一个空数组。
import React from "react";
import { useEffect, useState } from "react";
import { useLocation, Link } from "react-router-dom";
import { baseURL } from "../../../Apis";
function Lectures() {
let temp = [];
const [courses, setcourses] = useState(()=> {return null});
useEffect(() => {
const fun = async (e) => {
const response = await fetch(`${baseURL}/user/profile`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
});
const json = await response.json();
if (json.success) {
setcourses(json.data.courses);
}
};
fun();
}, []);
if(courses){
courses.map((course)=>{
return FetchChapter(course._id);
})
}
// Now fetching chapters of each course
function FetchChapter(id){
const fun = async (e) => {
const response = await fetch(`${baseURL}/course/${id}?queryParam=0`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
});
const json = await response.json();
json.data.chapters.map((chapter)=>{
return FetchTopic(id,chapter._id);
});
};
fun();
}
function FetchTopic(id,chapter_id){
const fun = async (e) => {
const response = await fetch(`${baseURL}/course/${id}/?
queryParam=1&chapterID=${chapter_id}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
});
const json = await response.json();
json.data.chapter.topics.map((topics)=>{
topics.subTopics.map((topic)=>{
FetchSubtopic(topic)
})
});
}
fun()
}
function FetchSubtopic(topic){
const fun = async (e) => {
const response = await fetch(`${baseURL}/course/subtopics/${topic._id}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
});
const json = await response.json();
if(json.contentType == 0){
temp.push(topic); // pushing all the subtopics into temp array
console.log(temp);
}
}
fun()
}
const [State, setState] = useState([]);
useEffect(() => {
console.log(temp); // Why empty?????
setState(temp);
}, []);
return (<> some code... </>)
}
export default Lectures;
I'm attempting to get the subtopics data, so first I have to get the courses in which a user has registered, then for each course I get the chapters, then for each chapter, I get the topics, and finally, I get the subtopics from each topic and push them all into the temp array.
However, when I tried to print it in the second useEffect, it returned an empty array despite printing in the FetchSubtopics function.
import React from "react";
import { useEffect, useState } from "react";
import { useLocation, Link } from "react-router-dom";
import { baseURL } from "../../../Apis";
function Lectures() {
let temp = [];
const [courses, setcourses] = useState(()=> {return null});
useEffect(() => {
const fun = async (e) => {
const response = await fetch(`${baseURL}/user/profile`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
});
const json = await response.json();
if (json.success) {
setcourses(json.data.courses);
}
};
fun();
}, []);
if(courses){
courses.map((course)=>{
return FetchChapter(course._id);
})
}
// Now fetching chapters of each course
function FetchChapter(id){
const fun = async (e) => {
const response = await fetch(`${baseURL}/course/${id}?queryParam=0`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
});
const json = await response.json();
json.data.chapters.map((chapter)=>{
return FetchTopic(id,chapter._id);
});
};
fun();
}
function FetchTopic(id,chapter_id){
const fun = async (e) => {
const response = await fetch(`${baseURL}/course/${id}/?
queryParam=1&chapterID=${chapter_id}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
});
const json = await response.json();
json.data.chapter.topics.map((topics)=>{
topics.subTopics.map((topic)=>{
FetchSubtopic(topic)
})
});
}
fun()
}
function FetchSubtopic(topic){
const fun = async (e) => {
const response = await fetch(`${baseURL}/course/subtopics/${topic._id}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
});
const json = await response.json();
if(json.contentType == 0){
temp.push(topic); // pushing all the subtopics into temp array
console.log(temp);
}
}
fun()
}
const [State, setState] = useState([]);
useEffect(() => {
console.log(temp); // Why empty?????
setState(temp);
}, []);
return (<> some code... </>)
}
export default Lectures;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您应该从内部 fun 而不是
使用
使用使用使用 useeffect 。使用
使用用空数组
[] 依赖性等于componentdidmount
。因此,一旦安装了组件,它就会被称为&amp;不要等待
提取
完成。I think you should call
setState(temp)
from insidefun
rather than inside anuseEffect
.An
useEffect
with an empty array[]
dependency is equivalent tocomponentDidMount
.Hence it's called as soon as the component is mounted & does not wait for the
fetch
to complete.由于JavaScript的事件循环,推动方法在您的情况下无法使用。
二人组合功能的异步行为,在堆栈更新您的数组之前,功能会启动功能。
Because of the JavaScript's event loop the push method won't work in your scenario.
Duo to the async behavior of your functions the function fires off before the stack updates your array.