Skip to content
This repository was archived by the owner on Jan 1, 2025. It is now read-only.
This repository was archived by the owner on Jan 1, 2025. It is now read-only.

Add lint rule for useRecoilCallback deps array #180

@itsMapleLeaf

Description

@itsMapleLeaf

Custom hooks that have custom dependency arrays can't be statically analyzed by the react-hooks linter rule, and can't have their dependencies automatically tracked. Because of this, I'd suggest either:

  1. Accept a callback and add that callback to the deps array of the internal useCallback. It'll make the callback on each render, or if the passed callback is useCallback'd, whenever the passed callback is recreated

  2. Use the 'effect ref' pattern, so the end user can always pass a flat callback, but the callback function doesn't have to be a part of the deps array:

    function useRecoilCallback(fn) {
      const fnRef = useRef(fn)
      useEffect(() => {
        fnRef.current = fn
      })
    
      return useCallback(() => {
        // read fnRef.current instead of fn
      }, [some, deps])
    }

I think 1. is "more correct" and would result in less bugs both internally and for the end user, but 2. would result in better UX, since 1. would require the user to useCallback/useMemo themselves if they wanted a properly recreated callback. A note in the docs about that might help, but those are fairly easy to miss 😅

Either way, I think this change will give a better UX when working with the hooks lint rule, with less surprises in general

Metadata

Metadata

Assignees

Labels

documentationImprovements or additions to documentationenhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions