Skip to content

Electron Reactive Event

Type-safe Electron IPC functions, including modern React hooks.

bash
npm install electron-reactive-event --save
Electron Reactive Event Logo, Hero-Sized.

Say goodbye to ...args: any[]

Once you create an event declaration type, the functions to send and receive your events will conform to the argument, response, and error types in your declaration.

Learn more about defining events

Intellisense

typescript
function send(Channel: "GetBoxData", Request: Box): Promise<BoxData>;
typescript
type Box = { X: number; Y: number; ... };
typescript
const Result: BoxData = await send("GetBoxData", { X: 

Flexible event types

When you define your events as types, you are free to include or omit types for request data, response data, and error payload data.

Don't need to send data with your request? No Request argument needed.

Don't need to respond with data? No return statement needed.

See how to register callbacks

Intellisense

typescript
function On(Channel: "GetPlayerIDs", Callback: ...): Promise<void>;
typescript
type Callback = () => Promise<Array<number>>;
typescript
On("GetPlayerIDs", async () =>
{
    const PlayerIDs: Array<number> = // ...

    if (PlayerIDs.length > 0)
    {
        return PlayerIDs
    }
    else
    {
        throw new ReactiveError("NoPlayersFound");
    }
});

Works seamlessly with React

The hooks provided by electron-reactive-event support modern features, including suspending and transitions.

Most hooks have Deferred counterparts, which are async and open up more options for interacting with main.

Browse the React hooks

Intellisense

typescript
function useSend(Channel: "SetPlayerName", Request: string, Suspend: boolean = false): ...;
typescript
const { Error, IsPending } = useSend("SetPlayerName", "f1ux", true

Fine-grained error types

For simple events, define your event's errors as string types to convey a simple message.

For events that need it, you can define your events as having an error payload type, to convey deeper information about erroneous state.

Browse examples of errors with payload types

Intellisense

typescript
function On(..., Callback: Callback<"UploadSubmission">): Promise<void>;
typescript
type Callback = (Request: Setting) => Promise<...>;
typescript
On("Resubmit", async () =>
{
    try
    {
        const Api = await GetSubmissionApi();
        const Recent = Api.GetRecent();

        await Api.Submit(Recent);
    }
    catch (SubmitError: unknown)
    {
        if (SubmitError instanceof SubmissionError)
        {
            throw new ReactiveError(
                "SubmitApiFailed",
                SubmitError.Reason
            );
        }

        throw new ReactiveError("Unspecified");
    }
});

Let the CLI do the boilerplate for you

Still not convinced? Even the boilerplate is handled for you, with our (optional) CLI tool. Call it when you create new event types, or include it in your build step.

Read the CLI setup guide

ts
$ npx electron-reactive-event-cli register

> electron-reactive-event (version 1.1.0)
> register

✓ Found configuration file in default location!
✓ Found 49 event declarations across 30 modules!
✓ Generated declaration module from your events!
Saved new module at ./src/registrars.types.ts!

The CLI updated your event registrars successfully!

$