Electron-Forge React 自定义钩子未调用 API。

huangapple go评论63阅读模式
英文:

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() },[]);

huangapple
  • 本文由 发表于 2023年6月22日 20:34:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定