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'
}
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:
hook-no-new-object-as-paramThe following patterns are considered warnings:
The following patterns are not considered warnings:
Additionally, we should validate that every value returned from custom hooks, is either:
useCallback,useMemo) or other custom hooksFor example:
The following patterns are considered warnings:
The following patterns are not considered warnings: