1. context1. Usage scenarios Imagine a scenario where we want to pass values to descendant components, what should we do? If you use props to pass it down layer by layer, it will be very cumbersome! A better way: use 2. Usage steps
const { Provider, Consumer } = React.createContext()
<Provider> <div className="App"> <Child1 /> </div> </Provider>
<Provider value="pink">
<Consumer> {data => <span>The data parameter represents the received data-- {data}</span>} </Consumer> 3. Conclusion
2. Props in-depth1. The children property children attribute: represents the child nodes of the component tag. When the component tag has child nodes, props will have this attribute The children property is the same as normal props, and its value can be anything (text, labels, components, or even functions) The code is as follows (example): function Hello(props) { return ( <div> Component's child nodes: {props.children} </div> ) } <Hello>I am a child node</Hello> 2. Props validation For components, props is a container for external data, and there is no guarantee of the format of data passed in by component users. If the format of the incoming data is incorrect, it may cause an error in the component Key issue: No additional error messages other than syntax error messages The code is as follows (example): // Created component function App(props) { const arr = props.colors const list = arr.map((item, index) => <li key={index}>{item}</li>) return ( <ul>{list}</ul> ) } // When using components <App colors={19} /> Props validation : allows you to specify the type and format of props when creating a component Purpose : Capture errors caused by props when using components, give clear error prompts, and increase the robustness of components 3. Props validation usage steps
import PropTypes from 'prop-types' function App(props) { return ( <h1>Hi, {props.colors}</h1> ) } App.propTypes = { // The colors property is agreed to be of array type // If the type is incorrect, a clear error will be reported to facilitate error analysis colors: PropTypes.array } 4. Props validation constraint rules Common types: React element type: Required: Objects of a specific structure: // Common type optionalFunc: PropTypes.func, // RequiredFunc: PropTypes.func.isRequired, // Object of specific structure optionalObjectWithShape: PropTypes.shape({ color: PropTypes.string, fontSize: PropTypes.number }) 5. Default props values Scenario : Pagination component → Number of items displayed per page Function : Set default values for props, which will take effect when no props are passed in function App(props) { return ( <div> The default value of props is shown here: {props.pageSize} </div> ) } // Set default values App.defaultProps = { pageSize: 10 } // Do not pass in the pageSize attribute <App /> SummarizeThis article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: The relationship between web page production and steamed buns (sharing experience)
>>: Implementation of CSS scroll bar style settings
People who use virtual machines usually set up sh...
Today, when we were learning about the Niu Nan new...
This article shares the specific code for JavaScr...
This article will introduce how to use explain to...
In most application scenarios, we need to back up...
xml <?xml version="1.0" encoding=&qu...
1. Basic use <!DOCTYPE html> <html lang=...
Phenomenon: Run an image, for example, ubuntu14.0...
This article shares the specific code of JavaScri...
1. Use frameset, frame and iframe to realize mult...
Table of contents Index Model B+Tree Index select...
Table of contents question 1. Install webpack web...
1. HTML tags with attributes XML/HTML CodeCopy co...
Suppose Taobao encourages people to shop during D...
Implement div wheel zooming in and out in Vue pro...