英文:
Electron-Forge react custom hook not invoking API
问题
我正在尝试构建一个桌面应用程序,用于从在线电子学习门户之一下载课程。我正在使用Electron Forge与React和Webpack创建用户界面。在本地开发环境中,我可以下载课程。但是,当我尝试从使用npm run make
创建的Mac可执行文件(dmg)下载课程时,我的自定义钩子未被执行。
import React, { useState, useEffect, useContext } from "react";
import { Context } from "../context/context";
export default function useFetchCourseData(courseId) {
console.log("useFetchCourseData");
const [courseData, setCourseData] = useState([]);
const [lectureCount, setLectureCount] = useState([]);
let { token } = useContext(Context);
const fetchCourseData = async () => {
console.log("fetchCourseData API CAll");
await fetch(
'URL',
{
headers: {
Authorization: `Bearer ${token}`,
},
}
)
.then((res) => res.json())
.then(({ results }) => {
const courseData = {};
let current;
let lectureCount = 0;
results.forEach((item, index) => {
if (item._class === "chapter") {
current = index;
courseData[index] = {};
courseData[index]["meta"] = item;
} else if (item._class === "lecture") {
lectureCount += 1;
if (courseData[current]["lectures"] === undefined)
courseData[current]["lectures"] = {};
courseData[current]["lectures"][index] = item;
}
});
setCourseData(courseData);
setLectureCount(lectureCount);
});
};
useEffect(() => fetchCourseData,[]);
return [courseData, lectureCount];
}
这里的useEffect
没有调用fetchCourseData
方法,在开发模式中正常工作。
英文:
I am trying to build a desktop application to download courses from one of the online e-learning portals. I am using electron forge with React and Webpack to create UI. I am able to download the courses in my local development environment. But, my custom hook is not getting executed when I try to download the course from Mac executable file (dmg) which was created using npm run make
.
import React, { useState, useEffect, useContext } from "react";
import { Context } from "../context/context";
export default function useFetchCourseData(courseId) {
console.log("useFetchCourseData");
const [courseData, setCourseData] = useState([]);
const [lectureCount, setLectureCount] = useState([]);
let { token } = useContext(Context);
const fetchCourseData = async () => {
console.log("fetchCourseData API CAll");
await fetch(
'URL',
{
headers: {
Authorization: `Bearer ${token}`,
},
}
)
.then((res) => res.json())
.then(({ results }) => {
const courseData = {};
let current;
let lectureCount = 0;
results.forEach((item, index) => {
if (item._class === "chapter") {
current = index;
courseData[index] = {};
courseData[index]["meta"] = item;
} else if (item._class === "lecture") {
lectureCount += 1;
if (courseData[current]["lectures"] === undefined)
courseData[current]["lectures"] = {};
courseData[current]["lectures"][index] = item;
}
});
setCourseData(courseData);
setLectureCount(lectureCount);
});
};
useEffect(() => fetchCourseData,[]);
return [courseData, lectureCount];
}
Here useEffect is not invoking the fetchCourseData method which is working fine in development mode.
答案1
得分: 1
尝试将您的 useEffect
从
useEffect(() => fetchCourseData,[]);
更改为
useEffect(() => { fetchCourseData() },[]);
英文:
Try changing your useEffect
from
useEffect(() => fetchCourseData,[]);
to
useEffect(() => { fetchCourseData() },[]);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论