简体   繁体   English

useEffect/useCallback 缺少依赖警告,但是 redux state 不让我修复它

[英]useEffect/useCallback missing dependency warnings, but redux state does not let me fix it

I'm trying to clean up my warnings, but im facing those dependency warnings.我正在尝试清理我的警告,但我面临着那些依赖性警告。

This is an example, but a lot of useEffect() is facing a similar problem.这是一个例子,但是很多 useEffect() 都面临着类似的问题。

Im trying to laod my page calling my fetch api inside useCallback (got samething inside useEffect), but the filter param there is actually a redux state我试图加载我的页面,在 useCallback 中调用我的 fetch api(在 useEffect 中得到相同的东西),但过滤器参数实际上是一个 redux state

 useEffect(() => {
    if (checkValidRoute(env.activeSelector.menu, "indicacoes")) {
      dispatch(
        indicationsAction.getIndications(config.page, config.rowsPerPage, config.order, {
          environmentId: env[router.query.ambiente].envId,
          loginId: user.login?.id,
          selectorID: env.activeSelector?.selectorID,
          token: user.login.token,
          details: false,
          filter: {
            status: config.status,
            dateInit: dateFormat(beforeMonth),
            dateEnd: dateFormat(today),
            name: config.name,
            indicatorName: config.indicatorName
          }
        })
      )
    } else {
      router.push(`/${router.query.ambiente}`)
    }
  }, [env, config.status, config.order, dispatch, beforeMonth, config.indicatorName, config.name, config.page, config.rowsPerPage, router, today, user.login?.id, user.login.token])

Those filters has it value associated to an input, i do not want to re-fetch after change my config state, because i need to wait for the user fill all the filter fields, but i need to reload my page if my env change.这些过滤器具有与输入关联的值,我不想在更改我的配置env后重新获取,因为我需要等待用户填写所有过滤器字段,但如果我的环境发生变化,我需要重新加载我的页面。

I thought about this solution, but it does not work我考虑过这个解决方案,但它不起作用

const filterParams = {
      page: config.page,
      rowsPerPage: config.rowsPerPage,
      order: config.order,
      details: false,
      filter: {
        status: config.status,
        dateInit: dateFormat(beforeMonth),
        dateEnd: dateFormat(today),
        name: config.name,
        indicatorName: config.indicatorName
    }
  }

  const loadPage = useCallback(() => {
    if (checkValidRoute(env.activeSelector.menu, "indicacoes")) {
      dispatch(
        indicationsAction.getIndications({
          environmentId: env[router.query.ambiente].envId,
          loginId: user.login?.id,
          selectorID: env.activeSelector?.selectorID,
          token: user.login.token,
        }, filterParams)
      )
    } else {
      router.push(`/${router.query.ambiente}`)
    }
  }, [dispatch, env, router, user.login?.id, user.login.token, filterParams])

  useEffect(() => {
    loadPage()
  }, [loadPage])

Now I got the following warning: The 'filterParams' object makes the dependencies of useCallback Hook (at line 112) change on every render. Move it inside the useCallback callback. Alternatively, wrap the initialization of 'filterParams' in its own useMemo() Hook.eslintreact-hooks/exhaustive-deps现在我收到以下警告: The 'filterParams' object makes the dependencies of useCallback Hook (at line 112) change on every render. Move it inside the useCallback callback. Alternatively, wrap the initialization of 'filterParams' in its own useMemo() Hook.eslintreact-hooks/exhaustive-deps The 'filterParams' object makes the dependencies of useCallback Hook (at line 112) change on every render. Move it inside the useCallback callback. Alternatively, wrap the initialization of 'filterParams' in its own useMemo() Hook.eslintreact-hooks/exhaustive-deps

if add filterParams to useMemo() dependencies samething will happend如果将filterParams添加到useMemo()依赖项,则会发生同样的事情

// eslint-disable-next-line react-hooks/exhaustive-deps sounds not good... // eslint-disable-next-line react-hooks/exhaustive-deps听起来不太好...

There's any solution for this?有什么解决办法吗? I think that I have to change my form to useForm() to get the onChange values then after submit() i set my redux state... but i dont know yet我认为我必须将我的表单更改为useForm()以获取onChange值,然后在submit()之后我设置我的 redux state...但我还不知道

EDIT: In that case i did understand that we need differente states to control my input state and my request state, they cant be equals.编辑:在那种情况下,我确实明白我们需要不同的状态来控制我的输入 state 和我的请求 state,它们不能相等。 If someone find another solution, i would appreciate (:如果有人找到另一种解决方案,我将不胜感激(:

EDIT2: Solved by that way: EDIT2:通过这种方式解决:

 const [ filtersState ] = useState(
    {
      page: config.page, 
      rowsPerPage: config.rowsPerPage, 
      order: config.order,
      data: {
        environmentId: env[router.query.ambiente].envId,
        loginId: user.login?.id,
        selectorID: env.activeSelector?.selectorID,
        token: user.login.token,
        details: false,
        filter: {
          status: config.status,
          dateInit: dateFormat(config.dateInit),
          dateEnd: dateFormat(config.dateEnd),
          name: config.name,
          indicatorName: config.indicatorName
        }
      }
    }
  );

  const handleLoadPage = useCallback(() => {
      if (checkValidRoute(env.activeSelector.menu, "indicacoes")) {
        dispatch(indicationsAction.getIndications({
          ...filtersState,
          filters: {
            ...filtersState.filters,
            selectorID: env.activeSelector?.selectorID,
          }
        }))
      } else {
        router.push(`/${router.query.ambiente}`)
      }
    }, [env.activeSelector, filtersState, dispatch, router]
  )

  useEffect(() => {
    handleLoadPage()
  }, [handleLoadPage])

Any other alternatives is appreciate任何其他选择都很感激

The thing here is, if you memoize something, it dependencies(if are in local scope) must be memoized too.这里的问题是,如果你记住了一些东西,它的依赖项(如果在本地范围内)也必须被记住。

I recommend you read this amazing article about useMemo and useCallback hooks.我建议您阅读这篇关于useMemouseCallback钩子的精彩文章。

To solve your problem you need to wrap filterParams within useMemo hook.要解决您的问题,您需要将filterParams包装在 useMemo 挂钩中。 And if one of it dependencies are in local scope, for example the dateFormat function, you'll need to wrap it as well.如果其中一个依赖项在本地 scope 中,例如dateFormat function,您还需要将其包装起来。

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

相关问题 反应 useEffect 和 useCallback linting 错误 - 缺少依赖项 - React useEffect and useCallback linting error - missing dependency React Hook useEffect/useCallback 缺少依赖项 - React Hook useEffect/useCallback has a missing dependency 在 useEffect 上使用方法有困难,缺少依赖项和 useCallback 错误? - Having difficulty using a method on useEffect, missing dependency and useCallback error? 如何修复 React Redux 和 React Hook useEffect 缺少依赖项:'dispatch' - How to fix React Redux and React Hook useEffect has a missing dependency: 'dispatch' 修复 React Hook useEffect 缺少依赖项 - Fix React Hook useEffect has a missing dependency React Hook useEffect 缺少依赖,使用 Redux - React Hook useEffect has a missing dependency, using Redux 使用 useEffect Hook 时如何修复缺少的依赖警告 - How to fix missing dependency warning when using useEffect Hook 如何解决此警告:“React Hook useEffect 缺少依赖项:'history'”? - How to fix this warning: “React Hook useEffect has a missing dependency: 'history'”? 如何使用 useEffect 修复 React 应用程序中缺少的依赖项? - How to fix missing dependency in React applications using useEffect? React Hook useEffect 缺少依赖项:'notes',如何修复? - React Hook useEffect has a missing dependency: 'notes', how to fix it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM