Skip to content

Create context template #15

@eddeee888

Description

@eddeee888

It would be awesome if we had the option to create context modules that export providers and hooks. I think most of the time, these files are boilerplates.

Input: ContextName
Produces:

  • a folder called <ContextName>Context
  • index file
  • <ContextName>Context.tsx
export * from './__ContextName__Context';
// <ContextName>Context.tsx
import React, { createContext, useContext } from 'react';

export interface __ContextName__Value {
}

const context = createContext<__ContextName__Value | null>(null);

export function __ContextName__Provider({ children }: __ContextName__Value & { children: React.ReactNode }) {
    return (
        <context.Provider value={{ }}>
            {children}
        </context.Provider>
    );
}

export function use__ContextName__(): __ContextName__Value {
    const value = useContext(context);
    
   if (!value) {
        throw new Error('use__ContextName__ must be used within a __ContextName__Provider');
    }

    return value;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions