useFilePicker icon indicating copy to clipboard operation
useFilePicker copied to clipboard

Hooks not wrapped with useCallback cause contant re-renders

Open JustMeGaaRa opened this issue 1 year ago • 1 comments

I use the "openFilePicker" function in my callback, and pass this callback to useEffect, and since "openFilePicker" is not wrapped in useCallback, the function constantly changes and triggers my useEffect, hence creating a constant rerendering of my UI.

Code sample:

`

const { openFilePicker } = useFilePicker({ accept: ".dsl", multiple: false });

const handleOnClickWorkspaceImport = useCallback(() => {
    openFilePicker();
}, [openFilePicker]);

useEffect(() => {
    setHeaderContent({
        right: (
            <ButtonGroup mr={4} variant={"filled"}>
                <IconButton
                    aria-label={"import workspace"}
                    colorScheme={"gray"}
                    icon={<Icon as={Upload} boxSize={5} />}
                    title={"Import Workspace"}
                    onClick={handleOnClickWorkspaceImport}
                />
            </ButtonGroup>
        )
    })
}, [setHeaderContent, handleOnClickWorkspaceImport]);

`

JustMeGaaRa avatar Feb 13 '24 11:02 JustMeGaaRa

Hi @JustMeGaaRa, thanks for spotting that, we'll fix this in the next patch release

MrKampla avatar Mar 11 '24 11:03 MrKampla

@JustMeGaaRa Fixed in v2.1.2. Please remember to wrap your props that you pass to useFilePicker with useMemo:

const ufpProps = useMemo(() => ({ accept: ".dsl", multiple: false }),[]);
const { openFilePicker } = useFilePicker(ufpProps);

This should result in a steady reference to openFilePicker function

MrKampla avatar Apr 24 '24 23:04 MrKampla