-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup
66 lines (48 loc) · 4.52 KB
/
backup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Context API Discription
The Context API in React is a mechanism for passing data through the component tree without the need to pass props manually at every level. It's designed to share data that can be considered "global" for a tree of React components.
Here's what the Context API does:
1. **Provider Component**: It provides a `Provider` component that allows you to wrap your component tree. This `Provider` component accepts a `value` prop, which can be any JavaScript value (object, string, array, etc.) that you want to share with child components.
2. **Consumer Component**: It provides a `Consumer` component or a `useContext` hook that allows child components to consume the value provided by the nearest `Provider` component in the component tree.
3. **Context Object**: It creates a context object using `React.createContext()` method. This object includes both `Provider` and `Consumer` components.
4. **Avoids Prop Drilling**: The Context API helps to avoid prop drilling, which is the process of passing props down through multiple layers of components. Instead of passing props manually from parent to child, components can directly access the shared data through context.
5. **Global State Management**: It's commonly used for managing global state such as user authentication, theme, language preferences, etc., where multiple components need access to the same data.
Here's a basic example of how Context API works:
```jsx
// Creating a context
const MyContext = React.createContext();
// Parent component providing value to its children
function ParentComponent() {
const sharedValue = "Hello from Context!";
return (
<MyContext.Provider value={sharedValue}>
<ChildComponent />
</MyContext.Provider>
);
}
// Child component consuming the context value
function ChildComponent() {
return (
<MyContext.Consumer>
{value => <div>{value}</div>}
</MyContext.Consumer>
);
}
```
In this example, `ParentComponent` provides the value "Hello from Context!" to its child `ChildComponent` using the `MyContext.Provider`. The `ChildComponent` consumes this value using the `MyContext.Consumer`.
1. **Import Statements**:
- `import React, { useState, createContext } from "react";`: This imports necessary functions and modules from the React library.
- `React`: The core library for building user interfaces in React.
- `useState`: A React hook that allows functional components to manage state.
- `createContext`: A function from React that creates a new context object.
2. **Context Creation**:
- `export const RestaurantsContext = createContext();`: This line creates a new context object named `RestaurantsContext` using the `createContext()` function. This context will be used to pass data to components without having to manually pass props through every level of the component tree.
3. **Context Provider Component**:
- `export const RestaurantsContextProvider = (props) => {`: This line defines a functional component named `RestaurantsContextProvider`. It takes `props` as input.
- `const [restaurants, setRestaurants] = useState([]);`: This line declares a state variable named `restaurants` using the `useState` hook. It initializes `restaurants` as an empty array and `setRestaurants` as the function to update the state. This state will hold the list of restaurants.
- `return ( ... );`: This is a JSX expression that returns the provider component.
- <RestaurantsContext.Provider value={{ restaurants, setRestaurants }}>: This line renders the `RestaurantsContext.Provider` component. It sets the `value` prop to an object containing the `restaurants` state and the `setRestaurants` function. This provides the context value to all components that are descendants of this provider.
- `{props.children}`: This is a special prop that represents the child components nested within the provider component. It allows components wrapped with `RestaurantsContextProvider` to access the context provided by it.
4. **Export Statements**:
- `export const RestaurantsContext`: This exports the `RestaurantsContext` object, allowing other components to import and use it.
- `export const RestaurantsContextProvider`: This exports the `RestaurantsContextProvider` component, making it available for use as a provider to access the context's value.
Overall, this code creates a context named `RestaurantsContext` and a corresponding provider component `RestaurantsContextProvider`. This provider component allows its descendant components to access and update the list of restaurants stored in the context's state.