Skip to content

Instantly share code, notes, and snippets.

@davidjbeaver
Last active November 12, 2021 07:17
Show Gist options
  • Save davidjbeaver/a0aa7eb27fbcb582433549448b5f9cef to your computer and use it in GitHub Desktop.
Save davidjbeaver/a0aa7eb27fbcb582433549448b5f9cef to your computer and use it in GitHub Desktop.
SolidJS Toast Tutorial - store.ts
import { nanoid } from "nanoid";
import { createStore } from "solid-js/store"
import { ToastObject, ToastType } from "./types";
const [toastStore, updateToastStore] = createStore({
toasts: [] as ToastObject[]
})
const addToast = (toastType: ToastType, message: string) => {
const toastId = nanoid();
updateToastStore("toasts", (t) => [{toastId, toastType, message}, ...t])
setTimeout(() => {
updateToastStore("toasts", (t) => t.filter((i) => i.toastId !== toastId));
}, 5000)
}
const removeToast = (toastId: string) => {
updateToastStore("toasts", (t) => t.filter((i) => i.toastId !== toastId));
}
export default {
toastStore,
addToast,
removeToast
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment