Last active
November 12, 2021 07:17
-
-
Save davidjbeaver/a0aa7eb27fbcb582433549448b5f9cef to your computer and use it in GitHub Desktop.
SolidJS Toast Tutorial - store.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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