| 
290 | 290 | |274| [What are loadable components?](#what-are-loadable-components)|  | 
291 | 291 | |275| [What is suspense component?](#what-is-suspense-component)|  | 
292 | 292 | |276| [What is route based code splitting?](#what-is-route-based-code-splitting)|  | 
 | 293 | +|277| [Give an example on How to use context?](#give-an-example-on-how-to-use-context)|  | 
 | 294 | +|278| [What is the purpose of default value in context?](#what-is-the-purpose-of-default-value-in-context)|  | 
 | 295 | +|279| [How do you use contextType?](#how-do-you-use-contexttype)|  | 
 | 296 | +|280| [What is a consumer?](#what-is-a-consumer)|  | 
 | 297 | +|281| [How do you solve performance corner cases while using context?](#how-do-you-solve-performance-corner-cases-while-using-context)|  | 
293 | 298 | 
 
  | 
294 | 299 | ## Core React  | 
295 | 300 | 
 
  | 
 | 
4530 | 4535 |      ```  | 
4531 | 4536 | 273. ### What is dynamic import?  | 
4532 | 4537 |      The dynamic import() syntax is a ECMAScript proposal not currently part of the language standard. It is expected to be accepted in the near future. You can achieve code-splitting into your app using dynamic import(). Let's take an example of addition,  | 
4533 |  | -     **Normal Import**  | 
 | 4538 | +     1. **Normal Import**  | 
4534 | 4539 |      ```javascript  | 
4535 | 4540 |      import { add } from './math';  | 
4536 | 4541 |      console.log(add(10, 20));  | 
4537 | 4542 |      ```  | 
4538 |  | -     **Dynamic Import**  | 
 | 4543 | +     2. **Dynamic Import**  | 
4539 | 4544 |      ```javascript  | 
4540 | 4545 |      import("./math").then(math => {  | 
4541 | 4546 |        console.log(math.add(10, 20));  | 
 | 
4594 | 4599 |      );  | 
4595 | 4600 |      ```  | 
4596 | 4601 |      In the above code, the code splitting will happen at each route level.  | 
 | 4602 | +277. ### Give an example on How to use context?  | 
 | 4603 | +     **Context** is designed to share data that can be considered **global** for a tree of React components.  For example, in the code below lets manually thread through a “theme” prop in order to style the Button component.  | 
 | 4604 | +     ```javascript  | 
 | 4605 | +     //Lets create a context with a default theme value "luna"  | 
 | 4606 | +     const ThemeContext = React.createContext('luna');  | 
 | 4607 | +     // Create App component where it uses provider to pass theme value in the tree  | 
 | 4608 | +     class App extends React.Component {  | 
 | 4609 | +       render() {  | 
 | 4610 | +         return (  | 
 | 4611 | +           <ThemeContext.Provider value="nova">  | 
 | 4612 | +             <Toolbar />  | 
 | 4613 | +           </ThemeContext.Provider>  | 
 | 4614 | +         );  | 
 | 4615 | +       }  | 
 | 4616 | +     }  | 
 | 4617 | +     // A middle component where you don't need to pass theme prop anymore  | 
 | 4618 | +     function Toolbar(props) {  | 
 | 4619 | +       return (  | 
 | 4620 | +         <div>  | 
 | 4621 | +           <ThemedButton />  | 
 | 4622 | +         </div>  | 
 | 4623 | +       );  | 
 | 4624 | +     }  | 
 | 4625 | +     // Lets read theme value in the button component to use  | 
 | 4626 | +     class ThemedButton extends React.Component {  | 
 | 4627 | +       static contextType = ThemeContext;  | 
 | 4628 | +       render() {  | 
 | 4629 | +         return <Button theme={this.context} />;  | 
 | 4630 | +       }  | 
 | 4631 | +     }  | 
 | 4632 | +     ```  | 
 | 4633 | +278. ### What is the purpose of default value in context?  | 
 | 4634 | +     The defaultValue argument is only used when a component does not have a matching Provider above it in the tree. This can be helpful for testing components in isolation without wrapping them. Below code snippet provides default theme value as Luna.  | 
 | 4635 | +     ```javascript  | 
 | 4636 | +     const MyContext = React.createContext(defaultValue);  | 
 | 4637 | +     ```  | 
 | 4638 | +279. ### How do you use contextType?  | 
 | 4639 | +     ContextType is used to consume the context object. The contextType property can be used in two ways,  | 
 | 4640 | +     1. **contextType as property of class:**  | 
 | 4641 | +     The contextType property on a class can be assigned a Context object created by React.createContext(). After that, you can consume the nearest current value of that Context type using this.context in any of the lifecycle methods and render function.  | 
 | 4642 | +     Lets assign contextType property on MyClass as below,  | 
 | 4643 | +     ```javascript  | 
 | 4644 | +     class MyClass extends React.Component {  | 
 | 4645 | +       componentDidMount() {  | 
 | 4646 | +         let value = this.context;  | 
 | 4647 | +         /* perform a side-effect at mount using the value of MyContext */  | 
 | 4648 | +       }  | 
 | 4649 | +       componentDidUpdate() {  | 
 | 4650 | +         let value = this.context;  | 
 | 4651 | +         /* ... */  | 
 | 4652 | +       }  | 
 | 4653 | +       componentWillUnmount() {  | 
 | 4654 | +         let value = this.context;  | 
 | 4655 | +         /* ... */  | 
 | 4656 | +       }  | 
 | 4657 | +       render() {  | 
 | 4658 | +         let value = this.context;  | 
 | 4659 | +         /* render something based on the value of MyContext */  | 
 | 4660 | +       }  | 
 | 4661 | +     }  | 
 | 4662 | +     MyClass.contextType = MyContext;  | 
 | 4663 | +     ```  | 
 | 4664 | +     2. **Static field**  | 
 | 4665 | +     You can use a static class field to initialize your contextType using public class field syntax.  | 
 | 4666 | +     ```javascript  | 
 | 4667 | +     class MyClass extends React.Component {  | 
 | 4668 | +       static contextType = MyContext;  | 
 | 4669 | +       render() {  | 
 | 4670 | +         let value = this.context;  | 
 | 4671 | +         /* render something based on the value */  | 
 | 4672 | +       }  | 
 | 4673 | +     }  | 
 | 4674 | +     ```  | 
 | 4675 | +280. ### What is a consumer?  | 
 | 4676 | +     A Consumer is a React component that subscribes to context changes. It requires a function as a child which receives current context value as argument and returns a react node. The value argument passed to the function will be equal to the value prop of the closest Provider for this context above in the tree. Lets take a simple example,  | 
 | 4677 | +     ```javascript  | 
 | 4678 | +     <MyContext.Consumer>  | 
 | 4679 | +       {value => /* render something based on the context value */}  | 
 | 4680 | +     </MyContext.Consumer>  | 
 | 4681 | +     ```  | 
 | 4682 | +281. ### How do you solve performance corner cases while using context?  | 
 | 4683 | +     The context uses reference identity to determine when to re-render, there are some gotchas that could trigger unintentional renders in consumers when a provider’s parent re-renders. For example, the code below will re-render all consumers every time the Provider re-renders because a new object is always created for value.  | 
 | 4684 | +     ```javascript  | 
 | 4685 | +     class App extends React.Component {  | 
 | 4686 | +       render() {  | 
 | 4687 | +         return (  | 
 | 4688 | +           <Provider value={{something: 'something'}}>  | 
 | 4689 | +             <Toolbar />  | 
 | 4690 | +           </Provider>  | 
 | 4691 | +         );  | 
 | 4692 | +       }  | 
 | 4693 | +     }  | 
 | 4694 | +     ```  | 
 | 4695 | +     This can be solved by lifting up the value to parent state,  | 
 | 4696 | +     ```javascript  | 
 | 4697 | +     class App extends React.Component {  | 
 | 4698 | +       constructor(props) {  | 
 | 4699 | +         super(props);  | 
 | 4700 | +         this.state = {  | 
 | 4701 | +           value: {something: 'something'},  | 
 | 4702 | +         };  | 
 | 4703 | +       }  | 
 | 4704 | + | 
 | 4705 | +       render() {  | 
 | 4706 | +         return (  | 
 | 4707 | +           <Provider value={this.state.value}>  | 
 | 4708 | +             <Toolbar />  | 
 | 4709 | +           </Provider>  | 
 | 4710 | +         );  | 
 | 4711 | +       }  | 
 | 4712 | +     }  | 
 | 4713 | +     ```  | 
0 commit comments