Skip to content

Avoid defining functions within components #23

Description

@eliasmalik

const randomise = array => {
array.sort(() => Math.random() - 0.5);
};

This function doesn't rely on any variables in its enclosing scope, so could be defined outside the component, which makes rendering more performant. Not a big deal for a small app, but good to know going forward.

If you did have a function that referenced variables in the enclosing scope (e.g. index), you can either make the function accept additional parameters or use useCallback which memoises functions defined within the component.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions