BOSC
How to Operate useCOntext Hook in React

How to Operate the “useContext” Hook In React.

The useContext hook in React assembles it comfortably to pass data to your app. Without holding them manually- deliver props down the tree.

It can create an easy choice for Redux when your data is superficial- or your app is short. Most React Hooks operations deliver elements credentials they didn’t have earlier.

Consumer elements hand data, but it is bulky to note extended functional code to operate this context API. So the UseContext hook enables the creation of the code readable, less verbose, and terminates the need to present a consumer element.

Syntax:

(const authContext = useContext(initialValue));

So if you are planning to outsource React js development services, then you should choose BOSC Tech Labs for all your IT outsourcing needs.

The useContext hook is slightly distinct, though- it just assembles things to look pleasant. In this article, we will read about operating the useContext in React.

syntax:

const authContext = useContext(initialValue);

Why Context in React?

Elements are the construction blocks of the consequence in React. These elements are illustrated in a tree hierarchy where an element is the parent of a child element.

The data gush in React is down the order, which is the data gush from parent element to child element and further down.

If the element is more in-depth in the hierarchy than the parent element, then the data is- passed through all the midpoint elements first. And then, it resolves conveniently to the element at the bottom of the order.

To prevent this type of design, operating redux as a library, some ReactJs developers perform on the global stock. And that supplies data or conditions globally. And without giving data or selecting a hierarchy, it is instantly attainable to all elements.

There are a few steps to execute react useContext in your project-

  • To assemble a reference object, create a file and export that reference object from that file.
[
theme-context.js
export const ThemeContext = React.createContext({
  theme: themes.dark,
  toggleTheme: () => {},
});
]

We have assembled and shipped a context object, and it receives a defaulting parameter or value that can depart to it.

If no prop is- passed through the parent element, it brings the default prop defined in the context object.

  • Now in a parent element whose value is to pass to the child element.

We will encase any child elements varying down the hierarchy in the comeback context provider.

Element A — Element B — Element C — Element D

  (predecessor) (child 1) (child 2) (child 3)

Here in the above line, element A is the parent element.
Element B is the child of element A.
Element C is the child of element B.
And Element D is the child of element C.

[
app.js
import {ThemeContext, themes} from './theme-context';
import ThemedButton from './themed-button';

function Toolbar(props) {
  return (
    
      Change Theme
    
  );
}

 

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      theme: themes.light,
    };
    this.toggleTheme = () => {
      this.setState(state => ({
        theme:
          state.theme === themes.dark
            ? themes.light
            : themes.dark,
      }));
    };
  }

  render() {
    return (
    );
  }
}
]

Here class App is the ancestor, the Toolbar is a child functional element, and ThemedButton is also a child component.

ThemedButton will take the default value of the context as it is- not wrap it inside any context provider.

We are done with the parent element and now move on to the child element definition.

  • In child elements, we can access values ​​in many manners.

Schedule an interview with React developers

Let’s examine the first method to access the value in the child element (context type).

[
Themed-button.js
Before Context
import {ThemeContext} from './theme-context';
class ThemedButton extends React.Element {
  render() {
    let props = this.props;
   let theme = this.props; // this.props used
   return (
      
    );
  }
}
ThemedButton.contextType = ThemeContext;
export default ThemedButton;
After Context
import {ThemeContext} from './theme-context';
import React from ‘react’
class ThemedButton extends React.Element {
  render() {
    let props = this.props;
   let theme = this.context; //Replaced by this.context
   return (
      
    );
  }
}
ThemedButton.contextType = ThemeContext;
export default ThemedButton;
]

Here ThemedButton is a child element and if you desire to access the value, then- use ContextType before shipping the segment.

So now we will see that- there is another way to access matters ​​using React Hooks.

[
import {ThemeContext} from './theme-context';
import React, {useContext} from ‘react’
class ThemedButton extends React.Component {
  render() {
   let theme = useContext(ThemeContext);
   return (
      
    );
  }
}
export default ThemedButton;
]

We used the useContext hook, and after that, we can instantly access the belongings as shown above.

Conclusion:

This is how we can operate with the “useContext” hook in react. We hope you got an idea about using the “useContext” hook in react. So, Thank you for reading our article. We hope you enjoyed our content. Moreover, please share your ideas with us, so that we can improve our content.