Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

usecontext hook react

import React, { useContext, createContext } from 'react'

const ThemeContext = React.createContext()

function Title() {
  const theme = useContext(ThemeContext)

  const style = {
    background: theme.primary,
    color: theme.text,
  }

  return <h1 style={style}>Title</h1>
}

function Nested() {
  return <Title />
}

function NestedTwice() {
  return <Nested />
}

export default function App() {
  const theme = {
    primary: 'dodgerblue',
    text: 'white',
  }

  return (
    <ThemeContext.Provider value={theme}>
      <NestedTwice />
    </ThemeContext.Provider>
  )
}
Comment

How to setup React Context, useContext Hook

export const AuthContext = createContext(INITIAL_STATE);

export const AuthContextProvider = ({ children }) => {
  const [state, dispatch] = useReducer(authReducer, INITIAL_STATE);

  useEffect(() => {
    localStorage.setItem("user", JSON.stringify(state.user));
  }, [state]);

  return (
    <AuthContext.Provider
      value={{
        user: state.user,
        isFetching: state.isFetching,
        error: state.error,
        dispatch,
      }}
    >
      {children}
    </AuthContext.Provider>
  );
};

export default AuthContext;
Comment

usecontext hook

import React, { useContext } from "react";
import ColorContext from "./colorcontex.ts";

const MyComponent = () => {
  const colors = useContext(ColorContext);

  return <div style={{ backgroundColor: colors.blue }}>...</div>;
};import React, { useContext } from "react";

const MyComponent = () => {
  const colors = useContext(ColorContext);

  return <div style={{ backgroundColor: colors.blue }}>...</div>;
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: convert json to excel in javascript 
Javascript :: add object in array state react 
Javascript :: angular custom directive 
Javascript :: how to install node js dependencies from package.json 
Javascript :: html get input box value by element id 
Javascript :: js get selected value by id 
Javascript :: jquery select direct child 
Javascript :: open new window in java script 
Javascript :: java script hash 
Javascript :: custom processing datatables 
Javascript :: search object array javascript 
Javascript :: javascript pop object from array 
Javascript :: switch window 
Javascript :: error first line of nextjs file 
Javascript :: get string length javascript 
Javascript :: how to export default class in javascript 
Javascript :: query parameters 
Javascript :: javascript prevent value change in select option 
Javascript :: how to make javascript function consise 
Javascript :: redux action 
Javascript :: when programmers net goes down 
Javascript :: how to calculate time taken for ajax call in javascript 
Javascript :: react usestate 
Javascript :: key js 
Javascript :: generate svg from javascript 
Javascript :: javascript return value from async function 
Javascript :: async await 
Javascript :: 15) Which of the following directive is used to initialize an angular app? A. ng-app ANSWER B.ng-model C.ng-controller D.None of the above 
Javascript :: how to check if a browser is supported 
Javascript :: what is functional programming 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =