React useContext Hook

Full Stack Developer with a passion for building web applications. PHP, Node.js, Laravel, MySQL, MongoDB. Love collaborating & making a difference
Search for a command to run...

Full Stack Developer with a passion for building web applications. PHP, Node.js, Laravel, MySQL, MongoDB. Love collaborating & making a difference
No comments yet. Be the first to comment.
Add it to the boot method of the AppServiceProvider of the app to improve Laravel developmet experience. use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\DB; class AppServiceProvider extends ServiceProvider { //... pub...

There is a plugin available for reactjs that one can use to add Calendly to Nextjs i.e., react-calendly. The problem is if you want to customize the button to open popup or modal then you need to have Calendly's pro plan for it. Here is the alternati...

After shifting to Ubuntu from Windows, I constantly look up for the alternatives of the tools that are available in Windows to enhance my arsenal in Linux. DbGate is the smartest SQL+noSQL database client. It works on Windows, Linux, MacOS and in web...

<div className="ratio ratio-16x9"> <video width="900" height="650" muted playsInline autoPlay loop> <source src="/images/test.mp4" type="video/mp4" /> </video> </div>

Creating middleware in Laravel is a common task that helps to filter HTTP requests entering your application. Middleware can perform various tasks such as authentication, logging, and modifying request/response objects. Step 1: Create Middleware You ...

useContext is a built-in React hook that allows you to consume values that are provided by a Context object. A Context provides a way to pass data through the component tree without having to pass props down manually at every level.
Here is a basic example of how to use useContext in a React component:
import React, { useState, createContext, useContext } from "react";
import ReactDOM from "react-dom/client";
export default function App() {
const MyContext = createContext();
function OuterComponent() {
const [value, setValue] = useState('test');
return (
<MyContext.Provider value={value}>
<h1>{`Hello ${value}!`}</h1>
<InnerComponent />
</MyContext.Provider>
);
}
function InnerComponent() {
return (
<>
<h1>Inner Component</h1>
<NestedComponent />
</>
);
}
function NestedComponent() {
const contextValue = useContext(MyContext);
return (
<>
<h1>Nested Component</h1>
<h2>{`Hello ${contextValue} again!`}</h2>
</>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<OuterComponent />);
}
In the example above, we create a Context object . Then, inside the NestedComponent function, we use the useContext hook to consume the value provided by the MyContext context. The contextValue variable now contains the value passed in by the nearest <MyContext.Provider> higher up in the component tree.
You can use the useContext hook to consume values from multiple contexts in the same component. Just call useContext with each context object you want to consume.