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?
I am currently using React Compiler in my app, which automatically handles the
useCallback,useMemoandmemooptimisations. However, the rule react-perf/jsx-no-new-function-as-prop is complaining when I am not wrapping with auseCallback, the functions declared within a component. So, for example, in the following component:I would like to have the rule detect this is wrong:
And to make me declare the function outside the JSX, like this:
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?