import React, { createContext, useContext } from 'react';
      
      export const filename = createContext({});
      export function usefilename() {
        return useContext(filename);
      }
      export const MyContextProvider = ({ children }: {children: React.ReactNode}) => {
        return (
          <filename.Provider
            value={{
          		val: 1
            }}
          >
            {children}
          </filename.Provider>
        );
      };
      export const usefilename = () => {
        const { val } = useContext(filename);
        return { val };
      };
      Typescript language logo
      your recipe card header background

      React create context with hook

      react
      context
      provider
      ...
      React TypeScript Recipes

      The code creates a context and a function that takes a context as an argument. The context defines the global scope for the function. The function uses the context to access the value of the val variable.

      Library: react

      Shortcut: react.hook.context.hook.create

      1 Comments

      • avatar

        alexmartx 4/14/2022, 3:02:46 AM

        0

        Thanks! I was looking for this snippet.

      Add Comment

      Log in to add a comment

      Codiga - All rights reserved 2022.