Skip to content

Type alias for Dispatch<SetStateAction<T>> #52

@brandongregoryscott

Description

@brandongregoryscott

A simple type alias around React's dispatch/set state action types would reduce verbosity while still getting the point across.

Something like this:

import { Dispatch, SetStateAction } from "react";

export alias type SetState<T> = Dispatch<SetStateAction<T>>;

Before:

const validate = (value: string, setError: Dispatch<SetStateAction<string>>) => {
    if (StringUtils.isEmpty(value)) {
        setError("Value cannot be empty!");
        return false;
    }

    return true;
}

const updateUser = (user: UserRecord, setContext: Dispatch<SetStateAction<UserContextRecord>>) => {
    // ... business logic ...
    setContext((prevState: UserContext) => prevState.with({ user }));
    return;
}

After:

const validate = (value: string, setError: SetState<string>) => {
    if (StringUtils.isEmpty(value)) {
        setError("Value cannot be empty!");
        return false;
    }

    return true;
}

const updateUser = (user: UserRecord, setContext: SetState<UserContextRecord>) => {
    // ... business logic ...
    setContext((prevState: UserContext) => prevState.with({ user }));
    return;
}

Open to other suggestions for naming convention, too - that was just my first thought

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions