Skip to content

Compatibility with React Compiler #79

@sofi-manomano

Description

@sofi-manomano

I am currently using React Compiler in my app, which automatically handles the useCallback, useMemo and memo optimisations. However, the rule react-perf/jsx-no-new-function-as-prop is complaining when I am not wrapping with a useCallback, the functions declared within a component. So, for example, in the following component:

export const Accordion = ({ children, title }: AccordionProps) => {
  const [isCollapsed, setCollapsed] = useState(true);
  const toggle = useCallback(() => setCollapsed((prev) => !prev), []);  <--------------- HERE

  return (
    <View>
      <Pressable onPress={toggle} style={[styles.header, !isCollapsed && styles.headerOpen]}>
          <Text>{title}</Typography>
      </Pressable>

      <View style={[styles.content, isCollapsed && styles.collapsed]}>
        {children}
      </View>
    </View>
  );
};

I would like to have the rule detect this is wrong:

<Pressable onPress={() => setCollapsed((prev) => !prev)}>

And to make me declare the function outside the JSX, like this:

const toggle = () => setCollapsed((prev) => !prev);
<Pressable onPress={toggle}>

But not have to wrap it with the unnecessary useCallback, since I have React Compiler configurated. So my request is, could you add a custom config for the rule to avoid creating functions within the render, but not require the use of useCallback?

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