USEPARAM外部反应组件

发布于 2025-02-11 09:33:07 字数 999 浏览 1 评论 0原文

我正在尝试传递使用UseParam Hook的变量值,以便将其传递到组件函数外部设置的API。

andelcomponent.js:

import React, { useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { fetchComponents } from "../../../features/componentsSlice";
import TreeItem from "@mui/lab/TreeItem";
import TreeView from "@mui/lab/TreeView";
import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
import ChevronRightIcon from "@mui/icons-material/ChevronRight";
import { Link, Outlet, useParams } from "react-router-dom";
import axios from "axios";
export const api = async () => {
  try {
    const res = await axios.get(
      // here
      `http://127.0.0.1:8000/api/maintenance/${vesselId}`
    );
    return res.data;
  } catch (error) {
    console.log(error);
  }
};
function VesselComponents() {
  // this line
  const vesselId = useParams();
  const { components, error, loading } = useSelector(
    (state) => state.components
  );
// rest of the code

I am trying to pass a variable value which uses useParam hook so i can pass it to my api which set outside of the component function.

VesselComponent.js :

import React, { useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { fetchComponents } from "../../../features/componentsSlice";
import TreeItem from "@mui/lab/TreeItem";
import TreeView from "@mui/lab/TreeView";
import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
import ChevronRightIcon from "@mui/icons-material/ChevronRight";
import { Link, Outlet, useParams } from "react-router-dom";
import axios from "axios";
export const api = async () => {
  try {
    const res = await axios.get(
      // here
      `http://127.0.0.1:8000/api/maintenance/${vesselId}`
    );
    return res.data;
  } catch (error) {
    console.log(error);
  }
};
function VesselComponents() {
  // this line
  const vesselId = useParams();
  const { components, error, loading } = useSelector(
    (state) => state.components
  );
// rest of the code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一身仙ぐ女味 2025-02-18 09:33:07

您可以尝试将param传递到api,可以帮助您从其他位置获得weastelid,包括useparams

export const api = async (vesselId) => {
  try {
    const res = await axios.get(
      // here
      `http://127.0.0.1:8000/api/maintenance/${vesselId}`
    );
    return res.data;
  } catch (error) {
    console.log(error);
  }
};

这就是我们如何称呼它的

const vesselId = useParams();
api(vesselId);

You can try to pass a param to api that would help you have vesselId from other places including useParams

export const api = async (vesselId) => {
  try {
    const res = await axios.get(
      // here
      `http://127.0.0.1:8000/api/maintenance/${vesselId}`
    );
    return res.data;
  } catch (error) {
    console.log(error);
  }
};

Here is how we call it

const vesselId = useParams();
api(vesselId);
帝王念 2025-02-18 09:33:07

您只能在组件内的顶层使用React钩。您不应在api函数中调用useParams。相反,您应该将其传递到api功能,并使用某些状态存储API的响应。这样的事情:

export const api = async (vesselId) => {
  try {
    const res = await axios.get(
      // here
      `http://127.0.0.1:8000/api/maintenance/${vesselId}`
    );
    return res.data;
  } catch (error) {
    console.log(error);
  }
};

function VesselComponents() {
  // this line
  const vesselId = useParams();
  const [vesselData, setVesselData] = useState();
  const { components, error, loading } = useSelector(
    (state) => state.components
  );

  const fetchVesselData = async () => {
    try {
    const res = await api(vesselId);
    setVessesData(res);
    } catch (e) {
      // handle error
    }
  }

  useEffect(() => {
    fetchVesselData()
  });

You can only use react hooks at the top level inside a component. You shouldn't call useParams in your api function. Instead, you should pass it to your api function and use some state to store the response from your API. Something like this:

export const api = async (vesselId) => {
  try {
    const res = await axios.get(
      // here
      `http://127.0.0.1:8000/api/maintenance/${vesselId}`
    );
    return res.data;
  } catch (error) {
    console.log(error);
  }
};

function VesselComponents() {
  // this line
  const vesselId = useParams();
  const [vesselData, setVesselData] = useState();
  const { components, error, loading } = useSelector(
    (state) => state.components
  );

  const fetchVesselData = async () => {
    try {
    const res = await api(vesselId);
    setVessesData(res);
    } catch (e) {
      // handle error
    }
  }

  useEffect(() => {
    fetchVesselData()
  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文