Home > Web Front-end > JS Tutorial > body text

What knowledge should I learn about react? Summary of react knowledge points (with complete examples)

寻∝梦
Release: 2018-09-11 16:01:50
Original
1722 people have browsed it

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, //Assign the value "abc" to the attribute name

b,

var props = {
  one:"123",
  two:"456"  
}
<HelloWorld {...props}/>  //展开语法相当于<HelloWorld one="123" two="456"}/>
c、var a = ReactDOM.render(<HelloWorld/>,document.body);
Copy after login

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:&#39;&#39;,                 }             },             render:function(){                 return <p>
                     <HelloWorld {...this.state}/>
                     <input type="text" onChange={this.handleChange} />
                 </p>             }         });         var a = ReactDOM.render(<HelloUniverse/>
         ,document.getElementById("root"));
Copy after login

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:&#39;&#39;,                     pwd:&#39;&#39;,                     gender:&#39;&#39;,                 }             },             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,&#39;name&#39;)}/>
                     <input type="text" onChange={this.handleChange.bind(this,&#39;pwd&#39;)}/>
                     <GenderSelect handleSelect={this.handleSelect}/>
                 </form>             }         });         var a = ReactDOM.render(<SignupForm />,document.getElementById("root"));
     </script>
 </body>
 </html>
Copy after login

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>
Copy after login

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:&#39;&#39;                 }             },             render:function(){                 return <p>
                     <input type="text" onChange={this.handleChange(&#39;text&#39;)} />
                     <p>{this.state.text}</p>
                 </p>             }         });         var a = ReactDOM.render(<Example />,document.getElementById("root"));
     </script>
 </body>
 </html>
Copy after login

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 as

react 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:&#39;example&#39;,
  }
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!