Skip to content

New rules: prevent unstable values passed to custom hooks, and returned from custom hooks #76

@huynhducduy

Description

@huynhducduy

Following the same principles, we should have 4 rules with the same characteristics as the current ones, but instead of validating jsx prop, they validate parameters passed to custom hooks.

For example:

  • Rule hook-no-new-object-as-param

The following patterns are considered warnings:

useFoo({})

useFoo(new Object())

useFoo(Object())

useFoo(this.props.config || {})

useFoo(this.props.config ? this.props.config : {})

useFoo({{display: 'none'}})

The following patterns are not considered warnings:

const staticConfig = {}

function useBar() {
    const stableObject = useMemo(() => {}, [])
    const shouldBeStableObject = useSomeOtherHook()

    useFoo(staticConfig)
    useFoo(stableObject)
    useFoo(shouldBeStableObject)
}

Additionally, we should validate that every value returned from custom hooks, is either:

  • Primitives
  • Stable (memoized) values come from the official hooks (useCallback, useMemo) or other custom hooks
  • Values come from the parameter

For example:

The following patterns are considered warnings:

function useFoo() {
    return {}
    return new Object()
    return Object()
}

The following patterns are not considered warnings:

function useFoo(param1) {
    return param1
    return useMemo(() => {}, [])
    return useCallback(() => {}, [])
    return useBar()
    return 'some string'
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions