svelte-create-context Svelte Themes

Svelte Create Context

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>

Top categories

Loading Svelte Themes