svelte-create-context
v1.0.2
Published
A keyless and typesafe approach to the Svelte context API
Downloads
90
Readme
svelte-create-context
A keyless and typesafe approach to the Svelte context API
💾 Install
npm i svelte-create-context
⚡ Quick example
// context.ts
import { createContext } from 'svelte-create-context';
export const {
setup: setupCurrentUser,
get: getCurrentUser
} = createContext<User>();
// parent component
<script lang="ts">
import { setupCurrentUser } from './context';
setupCurrentUser(new User());
</script>
// child component
<script lang="ts">
import { getCurrentUser } from './context';
const currentUser = getCurrentUser();
</script>
🔨 API
interface Context<T> {
setup: (value: T) => void;
get: () => T;
}
function createContext<T>(): Context<T>