


What knowledge should I learn about react? Summary of react knowledge points (with complete examples)
This article mainly introduces the learning about react, and summarizes the knowledge points about react. Let’s start reading the content of this article
Officially start learning react
1. If the first letter of a component in react is uppercase, it will be regarded as a custom component. If it is lowercase, it will be regarded as the DOM's own element name. If the first letter of your custom component name is lowercase, no error will be reported, but it will not be displayed.
2. There can only be one node in the outermost layer of the return of a custom component.
3. There cannot be statements in {} in the HTML you write, but there can be evaluation expressions. But you can write the statement in a function and then call the function in {}.
4. Function names and label names are named in camel case.
5. Use htmlFor and className. For example
6. Style writing: You can use var style = {color: "red", backgroundColor:" in jsx blue"} and then add style={style} in the custom tag. Remember to use camel case naming.
7. Non-DOM attributes:
a. dangerouslySetInnerHTML: insert HTML code directly into JSX
b. ref: parent component references child component
c. key: improve rendering performance. diff algorithm
8. Functions running in each life cycle of the component: a. Initialization.
b. Running.
c. Destroy.
9. Usage of attributes:
a,
b,
var props = { one:"123", two:"456" } <HelloWorld {...props}/> //展开语法相当于<HelloWorld one="123" two="456"}/> c、var a = ReactDOM.render(<HelloWorld/>,document.body);
a.setProps({name:"Tim"}); //This usage is not recommended, it violates the design principles of React (the latest version seems to have removed this function? Console.log comes out and grabs the prototype chain After searching, I couldn't find this function, only setState)
10. Usage of state:
var HelloWorld = React.createClass({ render:function(){ return <p>Hello,{this.props.name||"world"}</p> } }); var HelloUniverse = React.createClass({ handleChange:function(e){ this.setState({ name:e.target.value }); }, getInitialState:function(){ return { name:'', } }, render:function(){ return <p> <HelloWorld {...this.state}/> <input type="text" onChange={this.handleChange} /> </p> } }); var a = ReactDOM.render(<HelloUniverse/> ,document.getElementById("root"));
11. Properties and status Similarities and differences
12. Event processing function
13. Properties of event objects
14. Collaborative use of components
Collaborative use between father and son You can use child components to call methods of parent components. To achieve this goal, the parent component is passed to the child component through prop
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>Hello,world</title> <script src="../demo01/build/react.js"></script> <script src="../demo01/build/react-dom.js"></script> <script src="../demo01/browser.min.js"></script> </head> <body> <p id= "root"></p> <script type="text/babel"> var GenderSelect = React.createClass({ render:function(){ return <select name="gender" onChange={this.props.handleSelect}> <option value="1">男</option> <option value="0">女</option> </select> } }); var SignupForm = React.createClass({ getInitialState:function(){ return { name:'', pwd:'', gender:'', } }, handleChange:function(name,e){ var newState = {} newState[name] = e.target.value; this.setState(newState); }, handleSelect:function(e){ this.setState({gender:e.target.value}); }, render:function(){ console.log(this.state) return <form> <input type="text" onChange={this.handleChange.bind(this,'name')}/> <input type="text" onChange={this.handleChange.bind(this,'pwd')}/> <GenderSelect handleSelect={this.handleSelect}/> </form> } }); var a = ReactDOM.render(<SignupForm />,document.getElementById("root")); </script> </body> </html>
Parent-child component interaction(If you want to see more, go here PHP Chinese website React Reference Manual column to learn)
The sibling components can be implemented by passing the child component A to the parent component, and the parent component then passes it to the child component B.
15, mixin
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>Hello,world</title> <script src="../demo01/build/react.js"></script> <script src="../demo01/build/react-dom.js"></script> <script src="../demo01/browser.min.js"></script> </head> <body> <p id= "root"></p> <script type="text/babel"> var SetInit = { handleClick:function(e){ console.log(e.target.value); } } var Hello = React.createClass({ //这里命名必须为mixins mixins:[SetInit], render:function(){ return <input type="button" onClick={this.handleClick} value="123123"/> } }); var a = ReactDOM.render(<Hello />,document.getElementById("root")); </script> </body> </html>
mixin example
Advantages and Disadvantages:
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>Hello,world</title> <script src="../demo01/build/react.js"></script> <script src="../demo01/build/react-dom.js"></script> <script src="../demo01/browser.min.js"></script> </head> <body> <p id= "root"></p> <script type="text/babel"> var BindingMixin = { handleChange:function(name){ var that = this; return function(e){ var news = {}; news[name] = e.target.value; that.setState(news); } } }; var Example = React.createClass({ //这里命名必须为mixins mixins:[BindingMixin], getInitialState:function(){ return { text:'' } }, render:function(){ return <p> <input type="text" onChange={this.handleChange('text')} /> <p>{this.state.text}</p> </p> } }); var a = ReactDOM.render(<Example />,document.getElementById("root")); </script> </body> </html>
mixin
16. Controllable components and uncontrollable components
Controllable components have no value Hard-coded, such as value={this.state.value}.
Uncontrollable is the opposite.
Try to use controllable components
Problems encountered:
1. In the wepack.config.js configuration item, because the loader in the module has multiple configuration items , so it should be loaders, but I wrote loader, which caused the subsequent configuration items to not take effect and many compilation problems occurred. . .
2. In the return tag in the render of the component, forget to type / at the end of the tag. For example,
is written asreact will recognize it as two If a p tag is used, it will report embedded: Unterminated JSX contents.
3. All unpaired tags in render must be closed, such as:
otherwise An error will be reported: embedded: Expected corresponding JSX closing tag for
4. A very interesting thing is that if I setState a certain attribute in a certain function, then the attribute will not be printed out immediately. correct result. The correct result is to be in the componentDidUpdate function, that is, wait until the component is updated before printing it out.
5. If the prop of the child component is updated in the parent component, please do not put this prop into the getInitialState function as a property, because if the prop is updated, the child component will not update the properties in the state. (You can view the table in 11).
6. If you use es6 syntax, that is, use the method of inheriting React.Component to build components, you cannot use the getInitialState() function, and a warning will be reported: Warning: getInitialState was defined on TodoApp, a plain JavaScript class. This is only supported for classes created using React.createClass. Did you mean to define a state property instead?
Solution: Set constructor
constructor(props){ super(props); this.state = { example:'example', } }
This article ends here (if you want to see more, go to the PHP Chinese websiteReact User Manual column to learn ), if you have any questions, you can leave a message below.
The above is the detailed content of What knowledge should I learn about react? Summary of react knowledge points (with complete examples). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to build a real-time chat application using React and WebSocket Introduction: With the rapid development of the Internet, real-time communication has attracted more and more attention. Live chat apps have become an integral part of modern social and work life. This article will introduce how to build a simple real-time chat application using React and WebSocket, and provide specific code examples. 1. Technical preparation Before starting to build a real-time chat application, we need to prepare the following technologies and tools: React: one for building

React front-end and back-end separation guide: How to achieve front-end and back-end decoupling and independent deployment, specific code examples are required In today's web development environment, front-end and back-end separation has become a trend. By separating front-end and back-end code, development work can be made more flexible, efficient, and facilitate team collaboration. This article will introduce how to use React to achieve front-end and back-end separation, thereby achieving the goals of decoupling and independent deployment. First, we need to understand what front-end and back-end separation is. In the traditional web development model, the front-end and back-end are coupled

How to use React and Flask to build simple and easy-to-use web applications Introduction: With the development of the Internet, the needs of web applications are becoming more and more diverse and complex. In order to meet user requirements for ease of use and performance, it is becoming increasingly important to use modern technology stacks to build network applications. React and Flask are two very popular frameworks for front-end and back-end development, and they work well together to build simple and easy-to-use web applications. This article will detail how to leverage React and Flask

How to build a reliable messaging application with React and RabbitMQ Introduction: Modern applications need to support reliable messaging to achieve features such as real-time updates and data synchronization. React is a popular JavaScript library for building user interfaces, while RabbitMQ is a reliable messaging middleware. This article will introduce how to combine React and RabbitMQ to build a reliable messaging application, and provide specific code examples. RabbitMQ overview:

React code debugging guide: How to quickly locate and resolve front-end bugs Introduction: When developing React applications, you often encounter a variety of bugs that may crash the application or cause incorrect behavior. Therefore, mastering debugging skills is an essential ability for every React developer. This article will introduce some practical techniques for locating and solving front-end bugs, and provide specific code examples to help readers quickly locate and solve bugs in React applications. 1. Selection of debugging tools: In Re

ReactRouter User Guide: How to Implement Front-End Routing Control With the popularity of single-page applications, front-end routing has become an important part that cannot be ignored. As the most popular routing library in the React ecosystem, ReactRouter provides rich functions and easy-to-use APIs, making the implementation of front-end routing very simple and flexible. This article will introduce how to use ReactRouter and provide some specific code examples. To install ReactRouter first, we need

How to use React and Google BigQuery to build fast data analysis applications Introduction: In today's era of information explosion, data analysis has become an indispensable link in various industries. Among them, building fast and efficient data analysis applications has become the goal pursued by many companies and individuals. This article will introduce how to use React and Google BigQuery to build a fast data analysis application, and provide detailed code examples. 1. Overview React is a tool for building

How to use React and Docker to package and deploy front-end applications. Packaging and deployment of front-end applications is a very important part of project development. With the rapid development of modern front-end frameworks, React has become the first choice for many front-end developers. As a containerization solution, Docker can greatly simplify the application deployment process. This article will introduce how to use React and Docker to package and deploy front-end applications, and provide specific code examples. 1. Preparation Before starting, we need to install
