


What knowledge should I learn about react? Summary of react knowledge points (with complete examples)
Sep 11, 2018 pm 04:01 PMThis 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}/> //展開語(yǔ)法相當(dāng)于<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

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

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

Integration of Java framework and React framework: Steps: Set up the back-end Java framework. Create project structure. Configure build tools. Create React applications. Write REST API endpoints. Configure the communication mechanism. Practical case (SpringBoot+React): Java code: Define RESTfulAPI controller. React code: Get and display the data returned by the API.

PHP, Vue and React: How to choose the most suitable front-end framework? With the continuous development of Internet technology, front-end frameworks play a vital role in Web development. PHP, Vue and React are three representative front-end frameworks, each with its own unique characteristics and advantages. When choosing which front-end framework to use, developers need to make an informed decision based on project needs, team skills, and personal preferences. This article will compare the characteristics and uses of the three front-end frameworks PHP, Vue and React.

Vue.js is suitable for small and medium-sized projects and fast iterations, while React is suitable for large and complex applications. 1) Vue.js is easy to use and is suitable for situations where the team is insufficient or the project scale is small. 2) React has a richer ecosystem and is suitable for projects with high performance and complex functional needs.

Netflixusesacustomframeworkcalled"Gibbon"builtonReact,notReactorVuedirectly.1)TeamExperience:Choosebasedonfamiliarity.2)ProjectComplexity:Vueforsimplerprojects,Reactforcomplexones.3)CustomizationNeeds:Reactoffersmoreflexibility.4)Ecosystema

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

The React ecosystem includes state management libraries (such as Redux), routing libraries (such as ReactRouter), UI component libraries (such as Material-UI), testing tools (such as Jest), and building tools (such as Webpack). These tools work together to help developers develop and maintain applications efficiently, improve code quality and development efficiency.

Netflix uses React as its front-end framework. 1) React's componentized development model and strong ecosystem are the main reasons why Netflix chose it. 2) Through componentization, Netflix splits complex interfaces into manageable chunks such as video players, recommendation lists and user comments. 3) React's virtual DOM and component life cycle optimizes rendering efficiency and user interaction management.
