Skip to content

[compiler] Support annotating hook factories that produce stable hooks #31388

@ku8ar

Description

@ku8ar

What kind of issue is this?

  • React Compiler core (the JS output is incorrect, or your app works incorrectly after optimization)
  • babel-plugin-react-compiler (build issue installing or using the Babel plugin)
  • eslint-plugin-react-compiler (build issue installing or using the eslint plugin)
  • react-compiler-healthcheck (build issue installing or using the healthcheck script)

Link to repro

nope

Repro steps

The application I'm developing follows a specific pattern for accessing Redux store data based on parameters. Here's an example:

const ProductTile = ({ productId }) => {
  const useProductSelector = createUseSelectorWithParam(productId)
  const title = useProductSelector(productStore.selectLabel)
  const value = useProductSelector(productStore.selectValue)
  const brandId = useProductSelector(productStore.selectBrandId)

  const useBrandSelector = createUseSelectorWithParam(brandId)
  const subTitle = useBrandSelector(brandStore.selectLabel)

  return <Tile title={title} value={value} subTitle={subTitle} />
}

We currently use this "higher-order function" (HOF) approach in several thousand places across the codebase, with code like the following:

export const createUseSelectorWithParam = (param) => {
  const useSelectorWithParam = (selector) =>
    useSelector((state) => selector(state, param))

  return useSelectorWithParam
}

This approach reduces code complexity (fewer arrow functions) and enhances team productivity by decreasing informational noise. However, it currently lacks compatibility with the new React Compiler.

Question: Is there a way to inform the React Compiler that the result of createUseSelectorWithParam should be treated as a stable hook?

We're hesitant to replace thousands of instances across the codebase with arrow functions or to add parameters universally, as it would likely reduce readability (sometimes only one parameter is needed to access Redux store data, but other times two or even three are required). Additionally, with a higher number of parameters, making such extensive changes could lead to more bugs, as manually adjusting parameters in multiple places increases the chance of errors.

How often does this bug happen?

Every time

What version of React are you using?

18.2

What version of React Compiler are you using?

19.0.0-beta-6fc168f-20241025

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions