简体   繁体   English

如何设置useEffect来首先使用eslint-react-hooks从API获取数据?

[英]how to set an useEffect to fetch data from API at first render with eslint-react-hooks?

I need to call two functions to fetch data only on the first render. 我需要调用两个函数来仅在第一个渲染上获取数据。 I'm using react-hooks on this project. 我在这个项目上使用react-hooks。 So the code would look something like this: 所以代码看起来像这样:

const Component = (props) => {

  // init
  useEffect(() => {

    const fetchA = async () => {
      await props.fetchA()
    };

    const fetchB = async () => {
      await props.fetchB()
    };

    fetchA();
    fetchB();
  }, []);
}

fetchA and fetchB are actions performed by redux to make requests and save data on the reducers. fetchAfetchB是redux执行的操作,用于在reducers上发出请求和保存数据。
Then I added eslint-react-hooks to the project. 然后我在项目中添加了eslint-react-hooks Now eslint warns me that 现在,eslint警告我

Blockquote React Hook useEffect has a missing dependency: 'props'. Blockquote React Hook useEffect缺少依赖:'props'。 Either include it or remove the dependency array. 包括它或删除依赖项数组。 However, 'props' will change when any prop changes, so the preferred fix is to destructure the 'props' object outside of the useEffect call and refer to those specific props inside useEffect. 但是,当任何道具发生变化时,“道具”会发生变化,因此首选修复方法是在useEffect调用之外构造“道具”对象,并引用useEffect中的那些特定道具。

Is the only way to do this by applying // eslint-disable-next-line react-hooks/exhaustive-deps onto the line before the useEffect dependencies? 通过在useEffect依赖项之前将// eslint-disable-next-line react-hooks/exhaustive-deps到行上,这是唯一的方法吗?

It's pointing out that if props.fetchA and props.fetchB change, your code is not set up to update with it. 它指出如果props.fetchA和props.fetchB发生变化,你的代码就不会设置为随之更新。 If you're absolutely certain you want to ignore changes to props.fetchA and props.fetchB, then you can add an eslint-disable. 如果您完全确定要忽略对props.fetchA和props.fetchB的更改,则可以添加eslint-disable。

If you want to make your code perform updates when props.fetchA or props.fetchB change, then follow the instructions in the lint warning and do something like this: 如果要在props.fetchA或props.fetchB更改时使代码执行更新,请按照lint警告中的说明执行以下操作:

const { fetchA, fetchB } = props;
useEffect(() => {
  // i renamed these so as not to conflict with the outer variables. Feel free to choose different names.
  const a = async () => {/* fetchA() */};
  const b = async () => {/* fetchB() */};
  a();
  b();
}, [fetchA, fetchB]);

Depending on what fetchA and fetchB are doing, it's possible you'll need some cleanup logic to undo what was done the first time, but i can't tell you precisely what since i don't know what fetchA and fetchB do. 根据fetchA和fetchB正在做什么,你可能需要一些清理逻辑来撤消第一次做的事情,但我不能准确地告诉你什么,因为我不知道fetchA和fetchB做了什么。

暂无
暂无

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

相关问题 使用多个 React 钩子 useEffect 从 API 中获取数据,当第二次获取时使用第一个钩子中的数据 - Fetch data from API with multiple React hook useEffect when second fetch use data from first hook 如何在 React function 组件中不使用 useEffect 钩子来获取数据? - How to fetch data without useEffect hooks in React function component? React Hooks:在 useEffect 警告中获取数据 - React Hooks : fetch data inside useEffect warning 如何根据从第一次获得的值进行第二次 API 调用。 使用 React 和 useEffect 钩子 - How to Make A second API call based on the value gotten from the first. with React and useEffect hooks 如何使用反应钩子从第一个 api 调用结果中获取第二个 api 调用需要特定数据的序列 api 调用? - How to fetch sequence api calls where second api call need particular data from first api call result using react hooks? 与useEffect一起使用时如何防止触发useCallback(并遵守eslint-plugin-react-hooks)? - How to prevent useCallback from triggering when using with useEffect (and comply with eslint-plugin-react-hooks)? 在循环中使用 React useEffect 从 API 中多次获取数据 - Using React useEffect in a loop to fetch data from an API multiple times 在 useEffect 或事件处理程序中直接在反应中从 api 获取数据 - Fetch data from an api in useEffect or in event handler directly in react useReducer 和 useEffect 钩子来获取数据 - useReducer and useEffect hooks to fetch data React Hooks,state 未从 useEffect 中的 fetch 更新 - React Hooks, state not updating from fetch within useEffect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM