jsx-eslint / eslint-plugin-react

React-specific linting rules for ESLint
MIT License
8.86k stars 2.75k forks source link

with redux there is not necessary error #3764

Closed ravenkim closed 2 weeks ago

ravenkim commented 3 weeks ago

Is there an existing issue for this?

Description Overview

ESLint version

8.57.0

What problem do you want to solve?

When i use react redux i have two problem

    useEffect(() => {
        dispatch(routerAction.locationChange(route))
    }, [route])

problem 1 Void function return value is used

in routerAction.locationChange(route)

but actually

 locationChange: (state, action) => {
        state.location = action.payload
    },

In this code I do not need return

so I want this fixed

problem 2 ESLint: React Hook useEffect has a missing dependency: 'dispatch'. Either include it or remove the dependency array.(react-hooks/exhaustive-deps)

It can solve by put 'dispatch' inside [] but it is not needed

Redux's dispatch function is designed to have a constant reference throughout the app's life cycle. That is, even if a component is rerendered, the reference of the dispatch does not change. Therefore, you can prevent the hook from being unnecessarily re-executed without having to include the dispatch in the useEffect or useCallback dependency array.

So if (dependency is dispatch) {no warning plz} i hope there is extra rules for react users

What do you think is the correct solution?

no warning for theses two problem

Participation

Additional comments

No response

Expected Behavior

image

eslint-plugin-react version

7.34.1

eslint version

8.57.0

node version

20

ljharb commented 2 weeks ago

Neither of these are from the react plugin; you may want the react hooks plugin.

ravenkim commented 2 weeks ago

image i got only these extends, and when i remove

        'plugin:react/recommended',
        'plugin:react/jsx-runtime',
    theses two then error disappear

image and error show this error from eslint

ljharb commented 2 weeks ago

I’m not sure how that’d be the case; the react hooks plugin is what’s generating one of the warnings.

ravenkim commented 2 weeks ago

anyway, thanks for answer!

ravenkim commented 2 weeks ago

I hope this feature added!

 'react-hooks/exhaustive-deps': ['warn', {
            additionalHooks: '(useMyCustomHook|useAnotherCustomHook)',
            ignore: ['dispatch']
 }]
ljharb commented 2 weeks ago

That config references the react-hooks plugin, which is not this repo/project.

kimtaejin3 commented 2 weeks ago

@ljharb I have a question, what are your thoughts on that feature?

ljharb commented 2 weeks ago

@kimtaejin3 no feature was discussed; what are you referring to?

kimtaejin3 commented 2 weeks ago

@ljharb Many people may wonder if we should put the value in the dependence list of useEffect even for the same value that always remains unchanged. So I hope that the corresponding rule of react-hooks has a feature to warn only functions that are likely to change. I wonder if the method of implementing this feature is expensive and cumbersome, and whether this function is worth adding. I am sorry that I may not be able to communicate well because my English is not good

ljharb commented 2 weeks ago

@kimtaejin3 eslint-plugin-react-hooks is an entirely different package (one managed by the react team) than this one, eslint-plugin-react. Please file your issue on the react repo, not here.