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 :: split string into two parts javascript 
Javascript :: deleteone mongoose 
Javascript :: how click button and redirect angular 
Javascript :: declare function javascript 
Javascript :: adjust color of text js javascript 
Javascript :: is digit javascript 
Javascript :: on window resize and on page load 
Javascript :: Uncaught TypeError: $(...).jstree is not a function 
Javascript :: javascript push array with key name 
Javascript :: pass argument to event listener javascript 
Javascript :: use await in for each 
Javascript :: read image metadata javascript 
Javascript :: javascript event currenttarget 
Javascript :: scroll value bottom js 
Javascript :: react grid 
Javascript :: react.createelement 
Javascript :: regex check if number is greater than 
Javascript :: jquery dropdown selected value show field 
Javascript :: why to use event.persist 
Javascript :: show password fa-eye javascript 
Javascript :: right shift operator js 
Javascript :: javascript event listener 
Javascript :: express octet stream 
Javascript :: generate express app 
Javascript :: change class of icon using jquery 
Javascript :: useeffect cleanup in reactjs 
Javascript :: change console log to print javascript 
Javascript :: pause javascript 
Javascript :: redux update item in array 
Javascript :: how to check if a date has passed javascript 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =