Knowledge of react grammar

Yi Ya 2020-11-10 16:03:09
knowledge react grammar


created react After the project structure , We need to write a bunch of components , at present react There are two common ways to write components :

1, Function component :

import React from 'react';
function LogIn(props){
return(
<button> Sign in </button>
)
}
export default LogIn;

2,class Components :

import React from 'react';
class LoginControl from React.Component{
constructor(props){
// Data initialization 
super(props);
}
componentWillMount(){
// The data has been initialized , But it hasn't been rendered yet dom, Rarely used 
}
componentDidMount(){
// First render dom, Here you can ask for ajax, and setDate Re render the data 
}
componentWillReceiveProps(){
// After accepting the change of the parent component props When you need to re render components, you use more 
}
componentWillUnmount(){
// Unloading and data destruction 
}
render() {
/*js Handle */
return (
<div>
</div>
)
}
}
export default HomePage1;

Believe in JSX Rendering will not be strange , Not much here , Direct use {} Curly bracket rendering is OK ,  Note that it has to be effective JavaScript expression .

I'm going to highlight a point , stay react Event handling in , as well as props This important attribute , What's more, how to use and transfer parameters between child and parent components . The following is based on class Components :

Log in with the official website , Back out, for example :

First write a login and logout widget :

 Sign in :
import React from 'react';
function LogIn(props){
console.log(props)
return(
<button onClick={props.onClick}> Sign in </button>
)
}
export default LogIn;
 sign out :
import React from 'react';
function LogOut(props){
console.log(props)
return(
<button onClick={props.onClick}> sign out </button>
)
}
export default LogOut;

Next I'm going to write a login controller to render the components above :

import React from 'react';
import LogOut from "./LogOut";
import LogIn from "./LogIn";
class HomePage1 extends React.Component {
constructor(props){
// Data initialization 
super(props);
this.logIn = this.logIn.bind(this);
this.logOut = this.logOut.bind(this);
this.state = {
a:1,
b:2,
c:3,
isLogin:false,
user:null
}
}
componentWillMount(){
// The data has been initialized , But it hasn't been rendered yet dom, Rarely used 
}
componentDidMount(){
// First render dom, Here you can ask for ajax, and setDate Re render the data 
}
componentWillReceiveProps(){
// After accepting the change of the parent component props When you need to re render components, you use more 
}
componentWillUnmount(){
// Unloading and data destruction 
}
// Write any way you want to write ******************
logIn(e){
alert(" Log on to the ")
this.setState({
isLogin:true,
user:e
})
}
logOut(){
alert(" Log out ")
this.setState({
isLogin:false,
user:null
})
}
render() {
let button;
if(this.state.isLogin){
button = <LogOut onClick={this.logOut}/>
}else{
button = <LogIn onClick={(e)=>this.logIn('admin',e)}/>
}
return (
<div>
<h1>Hello,{this.state.a+this.state.b}</h1>
{button}
<p>{this.state.user}</p>
</div>
)
}
}
export default HomePage1;

among , I'll briefly talk about the problems in my study :

1. Just beginning to learn react, I don't know where to write my own way , After searching the documents and Du Niang , not have understood until then , Directly in class Just write in the component , Of course, the method is finished , To trigger him , How to trigger it next , Another problem ,react Two kinds of :

1.1 stay constructor Write in the build this.LogIn=this.LogIn.bind(this); Why do you write this way , The document is very clear , And then in return Start triggering in the element of onClick="{this.Login}" That's all right. ,,,

1.2 When writing methods , Use the arrow function ,Login = () =>{}, And then in return Start triggering in the element of onClick="{this.Login}" That's all right. ,,,

etc. ,,,,, What if I want to wear parameters ?? The official website also gives two ways :

<button onClick={(e) => this.deleteRow(id, e)}>Delete Row</button>
<button onClick={this.deleteRow.bind(this, id)}>Delete Row</button>

In both cases ,React The object of the event  e  Will be passed as a second parameter . If you use the arrow function , The event object must be passed explicitly , And by  bind  The way , Event objects and more parameters will be passed implicitly . ( I used to use the first method )

2. about props This attribute , The official website also put forward , Very important attributes , When I was a child, I wrote about this case ....props It is mainly used for parameter transfer between components .

2.1 Father and son pass on value .

. Down the value -- The parent component passes values to the child component
Parent component passed props Passing data that is not a method , Feed component ;

// Parent component 
<Child data={[1,2,3]} />
// Sub components 
console.log(this.props.data);

2. Up by value -- Child components pass values to parent components
Parent component passed props Pass a method to the subcomponent , The subcomponents call the method , And pass the data to the parent component in the form of parameters , The parent component can process the incoming data in this method ;

// Parent component 
import Child from './Child.js';
export default class Parent extend compenent{
getData=(data)=>{
console.log(data);
}
render(){
return (
<div>
Parent component
<Child getData={this.getData}/>
</div>
)
}
}
// Child components 
export default class Child extend compenent{
state={
data:[1,2,3]
}
render(){
const {data}=this.state;
return (
<div>
Child components
<button onClick={()=>{this.props.getData(data)}}><button>
</div>
)
}
}

 

 


 

版权声明
本文为[Yi Ya]所创,转载请带上原文链接,感谢

  1. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple