简体   繁体   English

如何修复 eslint 警告 JSX 属性值不应包含在同一 scope react-perf/jsx-no-new-function-as-prop 中创建的函数

[英]How to fix eslint warning warning JSX attribute values should not contain functions created in the same scope react-perf/jsx-no-new-function-as-prop

i am new to programming and learning typescript and i have been trying to understand and fix the eslint warning.我是编程和学习 typescript 的新手,我一直在尝试理解和修复 eslint 警告。 i have code like below,我有如下代码,

const handleRequestDelete = async () => { //eslint warning
    try {
        await deleteSomething({ variables: { ids }});
  
        onSuccess();
    } catch (error) {
        const [{ status, title, description }] =
        apolloErrorToNotifications(error);
        onError(error);
        throw error;
    }
};

on line const handleRequestDelete it throws eslint warning like so.在线 const handleRequestDelete 它会像这样抛出 eslint 警告。

warning  JSX attribute values should not contain functions created in the same scope  react-perf/jsx-no-new-function-as-prop

how could i fix this error.我该如何解决这个错误。 could someone help me.有人可以帮助我吗? thanks.谢谢。

That lint rule is pointing out that handleRequestDelete will be created new on every render.该 lint 规则指出,将在每次渲染时创建新的handleRequestDelete To satisfy the lint rule, you'll need to wrap your function in useCallback :为了满足 lint 规则,您需要将 function 包装在useCallback中:

const handleRequestDelete = useCallback(
  async () => {
    try {
      await deleteSomething({ variables: { ids } });

      onSuccess();
    } catch (error) {
      const [{ status, title, description }] =
        apolloErrorToNotifications(error);
      onError(error);
      throw error;
    }
  },
  [/* insert dependencies here */]
);

By doing this, then as long as the dependency array doesn't change, you'll reuse the same function on each render.通过这样做,只要依赖数组不改变,您将在每次渲染时重用相同的 function。 I'm not exactly sure what the dependency array needs to contain, but my guess is [deleteSomething, onSuccess, apolloErrorToNotifications, onError]我不确定依赖数组需要包含什么,但我的猜测是[deleteSomething, onSuccess, apolloErrorToNotifications, onError]

If you don't find this lint rule useful, you can disable it in your .eslint.json file, by adding this to the rules section:如果您觉得这条 lint 规则没有用,您可以在.eslint.json文件中禁用它,方法是将其添加到规则部分:

"rules": {
  "react-perf/jsx-no-new-function-as-prop": "off"
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复错误 jsx no new function as prop with typescript 并做出反应? - How to fix the error jsx no new function as prop with typescript and react? 36:16警告onSetOpen属性键的处理程序函数必须以'handle'react / jsx-handler-names开头 - 36:16 warning Handler function for onSetOpen prop key must begin with 'handle' react/jsx-handler-names 错误 react-jsx-dev-runtime.development.js:97 警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - Error react-jsx-dev-runtime.development.js:97 Warning: Each child in a list should have a unique "key" prop 传递onClick函数:[ESLINT] JSX道具不应该使用.bind()(react / jsx-no-bind) - Passing down onClick Functions: [ESLINT] JSX props should not use .bind() (react/jsx-no-bind) react-jsx-dev-runtime.development.js:97 警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - react-jsx-dev-runtime.development.js:97 Warning: Each child in a list should have a unique "key" prop 我应该如何修复 eslint 警告(React Hook useEffect 缺少依赖项)和由警告引起的循环? - How should I fix eslint warning (React Hook useEffect has a missing dependency) and a loop caused by the warning? 如何修复 vite 警告:无法设置 JSX 导入源? - How to fix vite warning: The JSX import source cannot be set? 此处不允许使用属性 jsx 警告 - WebStorm 中的样式 JSX - Attribute jsx not allowed here warning - styled JSX in WebStorm React-native - 提供回调 function 作为子组件的道具时发生 Lint 错误(JSX 道具不应使用函数) - React-native - Lint error while providing a callback function as a prop to child component (JSX props should not use functions) React Hooks 和 jsx-no-lambda 警告 - React Hooks and jsx-no-lambda warning
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM