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

svelte logo

Need a Svelte website built?

Hire a professional Svelte developer today.
Loading Svelte Themes