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

fix: toast not appearing when toast.* called from useEffect hook #251

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"@types/react": "^18.0.15",
"@types/react-dom": "^18.0.6",
"@types/testing-library__jest-dom": "^5.14.5",
"@types/use-sync-external-store": "^0.0.3",
"csstype": "^3.1.0",
"jest": "^28.1.3",
"jest-environment-jsdom": "^28.1.3",
Expand All @@ -94,7 +95,8 @@
"typescript": "^5.0.4"
},
"dependencies": {
"goober": "^2.1.10"
"goober": "^2.1.10",
"use-sync-external-store": "^1.2.0"
},
"peerDependencies": {
"react": ">=16",
Expand Down
21 changes: 18 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions src/core/store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react';
import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js';
import { DefaultToastOptions, Toast, ToastType } from './types';

const TOAST_LIMIT = 20;
Expand Down Expand Up @@ -157,14 +157,15 @@ export const reducer = (state: State, action: Action): State => {
}
};

const listeners: Array<(state: State) => void> = [];
const listeners: Array<() => void> = [];

let memoryState: State = { toasts: [], pausedAt: undefined };

export const dispatch = (action: Action) => {
const oldState = memoryState;
memoryState = reducer(memoryState, action);
listeners.forEach((listener) => {
listener(memoryState);
listener();
});
};

Expand All @@ -179,16 +180,16 @@ export const defaultTimeouts: {
};

export const useStore = (toastOptions: DefaultToastOptions = {}): State => {
const [state, setState] = useState<State>(memoryState);
useEffect(() => {
listeners.push(setState);
const state = useSyncExternalStore((onStoreChange) => {
listeners.push(onStoreChange);

return () => {
const index = listeners.indexOf(setState);
const index = listeners.indexOf(onStoreChange);
if (index > -1) {
listeners.splice(index, 1);
}
};
}, [state]);
}
}, () => memoryState);

const mergedToasts = state.toasts.map((t) => ({
...toastOptions,
Expand Down
26 changes: 25 additions & 1 deletion test/toast.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import {
render,
screen,
Expand Down Expand Up @@ -115,6 +115,30 @@ test('promise toast', async () => {
});
});

test('"toast" can be called from useEffect hook', async () => {
const WAIT_DELAY = 1000;

const MyComponent = () => {
const [success, setSuccess] = useState(false);
useEffect(() => {
toast.success("Success toast")
setSuccess(true);
}, []);

return success ? <div>MyComponent finished</div> : null;
}

render(
<>
<MyComponent />
<Toaster />
</>
);

await screen.findByText(/MyComponent finished/i);
expect(screen.queryByText(/Success toast/i)).toBeInTheDocument();
});

test('promise toast error', async () => {
const WAIT_DELAY = 1000;

Expand Down