...

/

useContext Hook

useContext Hook

Learn the "useContext" hook by building a basic application with it.

What is it?

useContext provides the current context value of the context object passed to it. The useContext hook relies on nearest Context Provider to determine the current value for that context.

Firstly, take a look at an example implementation using class and later convert it to use the useContext hook.

Assume you have an application with different theme colors depending upon the brand definition.

import React, { Component, createContext } from 'react';
const ThemeContext = createContext('green');

const App = () => {
  return (
    <ThemeContext.Provider value={'green'}>
      <Content />
   
...