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(MyContext);
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
import React, { createContext } from 'react';
import { themes } from './themes'; // Ensure themes is correctly imported
export const ThemeContext = 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 React, { useState } from 'react';
import { ThemeContext, themes } from './theme-context';
import ThemedButton from './themed-button';
function Toolbar(props) {
return (
Change Theme
);
}
function App() {
const [theme, setTheme] = useState(themes.dark);
const toggleTheme = () => {
setTheme((prevTheme) =>
prevTheme === themes.dark ? themes.light : themes.dark
);
};
return (
);
}
export default App;
}
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.
// ThemedButton.js import React from 'react'; import { ThemeContext } from './theme-context'; const ThemedButton = (props) => { const { theme, ...rest } = props; // Extract theme from props return ( <button {...rest} style={{ backgroundColor: theme.background, color: theme.foreground }} > {props.children} </button> ); };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 React, { useContext } from 'react'; import { ThemeContext } from './theme-context'; const ThemedButton = () => { const theme = useContext(ThemeContext); return ( <button style={{ backgroundColor: theme.background, color: theme.foreground }} > {/* Add your button content here */} Change Theme </button> ); };
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.