Context definition and purposeContext provides a way to share data between components without having to explicitly pass props down the component tree. What data needs to be shared in application scenarios?
How to use1. Create and initialize Context const MyContext = createContext(defaultValue); Create a Context object. When React renders a component that subscribes to this Context object, the component reads the current context value from the matching 2. Subscribe to Context <MyContext.Provider value={/* some value*/}> Provider receives a There are two related concepts here
3. Use Conext 3.1 Use in React components const value = useContext(MyContext); Reference the Context in the consuming component. value will read the current Context value from the matching Provider closest to itself in the component tree. 3.2 Use in pure functional components In a purely functional component, you can use <MyContext.Consumer> {value => /* render based on context value */} </MyContext.Consumer> 4. Context Update 4.1 Updating Context from Top to Bottom Top-down updating refers to updating the value of the Provider. When //App.js // .... export default function App() { //... // const {contextValue, setContextValue} = React.useState(initialValue); // function to update the context value function updateContext(newValue) { // ... // Updating contextValue, ConsumerComponent1, ConsumerComponent2, ConsumerComponent3, ConsumerComponent11 will trigger re-rendering. setContextValue(newValue) } ... return ( <App> <MyContext.Provider value={contextValue}> <ConsumerComponent1> <ConsumerComponent11> // .... </ConsumerComponent11> </ConsumerComponent1> <ConsumerComponent2 /> <ConsumerComponent3 /> </MyContext.Provider> </App> ); } 4.2 Bottom-up (from consuming components) Context update In some cases, // app.js export default function App() { ... const {contextValue, setContextValue} = React.useState(initialValue); // function to update the context value function updateContext(newValue) { // ... // Updating contextValue, ConsumerComponent1, ConsumerComponent2, ConsumerComponent3, ConsumerComponent11 will trigger re-rendering. setContextValue(newValue) } ... return ( <App> <MyContext.Provider value={contextValue}> <ConsumerComponent1> <ConsumerComponent11 updateValue={updateContext}> // Update contextValue in ConsumerComponent11 through callback props. Because contextValue belongs to the value of the top-level Provider, it will also trigger ConsumerComponent1, ConsumerComponent2, and ConsumerComponent3 to re-render. </ConsumerComponent11> </ConsumerComponent1> <ConsumerComponent2 /> <ConsumerComponent3 /> </MyContext.Provider> </App> ); } 4.3 Provider Nesting In some cases, the providers of the same Context may be nested, which can be understood as two Contexts. The difference is, ... const {contextValue, setContextValue} = React.useState(initialValue); // function to update the context value function updateContext(newValue) { // ... // Updating contextValue, ConsumerComponent1, ConsumerComponent2, ConsumerComponent3, ConsumerComponent11 will trigger re-rendering. setContextValue(newValue) } ... return ( <App> <MyContext.Provider value={contextValue}> <ConsumerComponent1> <ConsumerComponent11 /> </ConsumerComponent1> <ConsumerComponent2> ... // If you only want to update the values in ConsumerComponent21 and ConsumerComponent22 const localContextValue = useContext(MyContext); // Get the current value from the upper layer Provider const {tempContextValue, setTempContextValue} = React.useState(localContextValue); function updateTempContext(newValue) { // The update here will only trigger the re-rendering of ConsumerComponent21 and ConsumerComponent22 setTempContextValue(newValue); } // Create a new Provider here to share data between ConsumerComponent21 and ConsumerComponent22. <MyContext.Provider value={tempValue}> <ConsumerComponent21> // Subscribe in ConsumerComponent21 via useContext(MyContext) // The value obtained is the Context value read from the matching Provider closest to itself, that is, tempValue </ConsumerComponent21> <ConsumerComponent22> </ConsumerComponent22> </MyContext.Provider value={contextValue}> </ConsumerComponent2> <ConsumerComponent3 /> </MyContext.Provider> </App> ); Official Documentation For official documentation, please refer to the basic and advanced tutorials below. Hooks API Index – React (reactjs.org) Context – React (reactjs.org) The above is the detailed content of the analysis of Context application scenarios in React. For more information about Context in React, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Install MySQL in Ubuntu 18.04 (Graphical Tutorial)
>>: MySQL 8.0 download and installation configuration graphic tutorial under Windows 10
Let’s look at the effect first: This effect looks...
This article example shares the specific code of ...
Table of contents 1. Insert the queried results 2...
Today I will introduce a small Javascript animati...
engine Introduction Innodb engine The Innodb engi...
IDEA is the most commonly used development tool f...
question CSS fixed positioning position:fixed is ...
<style type="text/css"> Copy code ...
1. Abnormal performance of Docker startup: 1. The...
1. Docker installation and startup yum install ep...
When there are tens of thousands of records in th...
React originated as an internal project at Facebo...
The MySQL query result row field splicing can be ...
Preface Last week, a colleague asked me: "Br...
Concept of SFTP sftp is the abbreviation of Secur...