javascript - React Hook "useTranslation" is called in function "getMyMenu" that is neither a

I am invoking the useTranslation hook from within a normal function as belowimport { useTranslation }

I am invoking the useTranslation hook from within a normal function as below

import { useTranslation } from "react-i18next";


function getMyMenu(a) {
  const { t } = useTranslation('translations');
  
  if (a.showRoute) {
      a.label = t(a.label);
    return a;
  }
}



export const MyNav = props => {
  let tabs = recurseFn(getMyMenu, props.routes);
}

I get the following error. Is there no way to fix this? For some strange reason, I was able to see the similar code pattern working in other project. Any additional config needed to make this work?

Updated:

This is how my recurseFn looks like:

export const recurseFn = (chk, [head, ...tail]) => {
  if (head === undefined) return [];
  if (chk(head)) {
    return [head, ...recurseFn(chk, tail)];
  }

  return [...recurseFn(chk, tail)];
};

I am invoking the useTranslation hook from within a normal function as below

import { useTranslation } from "react-i18next";


function getMyMenu(a) {
  const { t } = useTranslation('translations');
  
  if (a.showRoute) {
      a.label = t(a.label);
    return a;
  }
}



export const MyNav = props => {
  let tabs = recurseFn(getMyMenu, props.routes);
}

I get the following error. Is there no way to fix this? For some strange reason, I was able to see the similar code pattern working in other project. Any additional config needed to make this work?

Updated:

This is how my recurseFn looks like:

export const recurseFn = (chk, [head, ...tail]) => {
  if (head === undefined) return [];
  if (chk(head)) {
    return [head, ...recurseFn(chk, tail)];
  }

  return [...recurseFn(chk, tail)];
};
Share Improve this question edited Mar 29, 2021 at 8:48 zhulien 5,7154 gold badges24 silver badges39 bronze badges asked Mar 29, 2021 at 6:56 copenndthagencopenndthagen 50.9k105 gold badges313 silver badges491 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You can fix it by moving the useTranslation hook to the MyNav ponent body and pass the translation function t as a closure in getMyMenu.

import { useTranslation } from "react-i18next";

export const MyNav = props => {
  const { t } = useTranslation('translations');

  function getMyMenu(a) {
    if (a.showRoute) {
      a.label = t(a.label);
      return a;
    }
  }

  let tabs = recurseFn(getMyMenu, props.routes);

  ...
}

Edit

I just updated with how my recurseFn looks like. Is it possible to pass "t" to it?

Solution 1 - Pass t explicitly to all functions

You could certainly update the function signature of recurseFn to also consume a "t" (translation?) function, but you'd still need to then also pass t into chk which would necessitate updating the original getMyMenu function to consume an additional argument.

Example:

function getMyMenu(t, a) { // <-- consume additional t argument
  if (a.showRoute) {
      a.label = t(a.label);
    return a;
  }
}

...

export const recurseFn = (chk, [head, ...tail], t) => { // <-- consume additional t argument
  if (head === undefined) return [];
  if (chk(t, head)) { // <-- pass t to chk
    return [head, ...recurseFn(chk, tail, t)]; // <-- pass t on recursion
  }

  return [...recurseFn(chk, tail, t)]; // <-- pass t on recursion
};

...

import { useTranslation } from "react-i18next";

export const MyNav = props => {
  const { t } = useTranslation('translations');

  let tabs = recurseFn(getMyMenu, props.routes, t); // <-- pass t on recursion

  ...
}

Solution 2 - A better solution using a curried callback

In this case I think making getMyMenu a curried function can help you enclose t in the callback and allow it to be declared externally to any React ponent. The recurseFn function doesn't need to know anything about t, so why expose it there, right?

const getMyMenu = t => a => {
  if (a.showRoute) {
    a.label = t(a.label);
    return a;
  }
}

Now you destructure t from the useTranslation hook return value and pass to the curried getMyMenu function. This is a similar idea to closing over the t function in the callback like we did above in the first answer.

import { useTranslation } from "react-i18next";

export const MyNav = props => {
  const { t } = useTranslation('translations');

  let tabs = recurseFn(getMyMenu(t), props.routes);

  ...
}

Now from recurseFn's perspective, t is enclosed in the chk callback and recurseFn didn't need to explicitly handle receiving and passing t around.

I was able to solve this same issue by ensuring that the function name was capitalized. The plaint explicitly stated that "React ponent names must start with a an uppercase letter." After correcting this issue the ESLint message vanished.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744183835a4562110.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信