Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR HTML

react createContext usage

import { createContext } from 'react';

const NameContext = createContext('');

function App() {
  return (
    <NameContext.Provider value="John Doe">
      <Body />
    <NameContext.Provider>
  );
} 

function Body() {
  return <Greeting />;
} 

function Greeting() {
  return (
    <NameContext.Consumer>
      {name => <h1>Welcome, {name}</h1>}
    </NameContext.Consumer>
  );
}
Source by www.freecodecamp.org #
 
PREVIOUS NEXT
Tagged: #react #createContext #usage
ADD COMMENT
Topic
Name
2+6 =