React create context with hook

    0

    9

    Jose Romero

    reactcontextproviderβ€’β€’β€’
    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

    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 };
    };
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    Legal
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.