您应该在react.useeffect()中调用navagiate(),而不是首先渲染组件时

发布于 2025-02-04 02:37:49 字数 1549 浏览 4 评论 0原文

我在上下文的一个功能之一中使用导航有问题。我有一个称为GetCurrencyFrompath的函数,该功能通过USECONTEXT提供给了我的组件。此功能在使用效果中,仅在组件的MOUNT上调用,以从查询参数获取货币(示例路径: /historingrates?currency = usd)。如果某人尝试将货币(查询参数)传递给我的应用程序无效,我想将用户导航到USD的默认货币。因此,在我的功能中,我有一个“ if”检查,如果查询参数还可以,如果不是,则导航。因此,当用户传递此错误的查询参数时,我的页面安装座,应立即调用,但这不是,但是我会收到此错误。如果我在使用效率下直接调用导航,则没有错误,但是一旦我尝试将此代码外包到上下文函数进行相同的逻辑,我就会遇到错误。

PS。我想将此逻辑保留在此上下文功能中,直接将其置于使用效果并不是我要实现的目标(尽管它可以解决问题)

// component

const Header: React.FC = () => {
  const { getCurrencyFromPath, currency, setCurrency } = React.useContext(HistoricalRatesContext);

  const navigate = useNavigate();

  React.useEffect(() => {
    getCurrencyFromPath();
  }, []);

  const onPickerValueChange = (currency: Currencies) => {
    navigate(`/HistoricalRates?currency=${currency}`);
    setCurrency(currency);
  };

  return (
    <div className={classes.chartHeader}>
      <PageHeader text={`USD historical rates`}></PageHeader>
      <CurrencyPicker className={classes.pickerAdditional} value={currency} 
       changeValue={onPickerValueChange} blockedCurrencies={[Currencies.PLN]} />
    </div>
  );
};

export default Header;

// context

import { useNavigate } from 'react-router-dom';

.
.
.

const navigate = useNavigate();

const getCurrencyFromPath = () => {
    let currency = query.get('currency') || '';

    if (!isValidCurrency(currency)) {
      currency = Currencies.USD;
      navigate(`/HistoricalRates?currency=${currency}`);
    }

    setCurrency(currency as Currencies);
  };



I have a problem with using navigate in one of my context's functions. I have a function called getCurrencyFromPath that is provided to my component via useContext. This function is called in useEffect, only on component's mount, to get a currency from query parameter (example path: /HistoricalRates?currency=USD). If someone would try to pass a currency (into query parameter) that is not valid for my app I'd like to navigate user to a default currency which is USD. So in my function I have an "if" check, if query parameter is ok, if not then navigate. So when user passes this wrong query parameter my page mounts and at once navigate should be called, but it's not and instead I get this error. If i call navigate directly in useEffect it works, there is no error, but as soon as i try to do the same logic with outsourcing this code to context function I get an error.

PS. I want to keep this logic in this context function, putting this directly in useEffect is not what I want to achieve (although it would solve the problem)

// component

const Header: React.FC = () => {
  const { getCurrencyFromPath, currency, setCurrency } = React.useContext(HistoricalRatesContext);

  const navigate = useNavigate();

  React.useEffect(() => {
    getCurrencyFromPath();
  }, []);

  const onPickerValueChange = (currency: Currencies) => {
    navigate(`/HistoricalRates?currency=${currency}`);
    setCurrency(currency);
  };

  return (
    <div className={classes.chartHeader}>
      <PageHeader text={`USD historical rates`}></PageHeader>
      <CurrencyPicker className={classes.pickerAdditional} value={currency} 
       changeValue={onPickerValueChange} blockedCurrencies={[Currencies.PLN]} />
    </div>
  );
};

export default Header;

// context

import { useNavigate } from 'react-router-dom';

.
.
.

const navigate = useNavigate();

const getCurrencyFromPath = () => {
    let currency = query.get('currency') || '';

    if (!isValidCurrency(currency)) {
      currency = Currencies.USD;
      navigate(`/HistoricalRates?currency=${currency}`);
    }

    setCurrency(currency as Currencies);
  };



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

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

发布评论

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

评论(1

荆棘i 2025-02-11 02:37:49

这部分不应该在此路线 /历史货币的组件中?货币= $ {货币}

  React.useEffect(() => {
    getCurrencyFromPath();
  }, []);

This part should'nt be in the component of this route /HistoricalRates?currency=${currency}

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