Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hooks not wrapped with useCallback cause contant re-renders #87

Closed
JustMeGaaRa opened this issue Feb 13, 2024 · 2 comments
Closed

Hooks not wrapped with useCallback cause contant re-renders #87

JustMeGaaRa opened this issue Feb 13, 2024 · 2 comments

Comments

@JustMeGaaRa
Copy link

JustMeGaaRa commented Feb 13, 2024

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]);

`

@MrKampla
Copy link
Collaborator

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

@MrKampla
Copy link
Collaborator

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants