Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
Providing values for react context twice on createContext and Provider component

React context usage with createContext does have an optional default value and most of the explanations on web are something like below.

import { createContext } from 'react';
const Context = createContext('Default Value');

then we have the Provider components on the context object which makes the child components capable to listen the context using Consumer components.

function Main() {
  const value = 'My Context Value';
  return (
    <Context.Provider value={value}>     /* Why is it mandatory to provide value attribute when we have the default value argument in createContext method. */
      <MyComponent />
    </Context.Provider>
  );
}

and then Using Consumers we can get the values without worrying about using the props way of passing data in React js.

<Context.Consumer>
      {value => <span>{value}</span>}   //context value in anonymous inner function
    </Context.Consumer>

Why do we need to separate values for context using the createContext method and then on Provider component ? can't we use the value passed to createContext method itself ? Thanks in advance.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!