Home > Article > Web Front-end > What are the basic syntaxes of React? Introduction to the basic syntax of react (with examples)
This article mainly introduces the basic syntax of react, as well as a detailed explanation of the initialization methods of state and props. Next, let us read this article together
1. What is React
2.React component
3 State and Props
4 React component life cycle
React is a JAVASCRIPT library used to build user interfaces.
React is mainly used to build UI. Many people think that React is the V (view) in MVC.
React originated as an internal project at Facebook, used to build the Instagram website, and was open sourced in May 2013.
React has high performance and very simple code logic. More and more people have begun to pay attention to and use it.
React uses JSX instead of regular JavaScript. JSX is a JavaScript syntax extension that looks a lot like XML.
Note
HTML tags in JSX use lowercase letters, and React components start with uppercase letters
Attributes are named using small camel case, the first one The first letter of the word is lowercase and the other uppercase letters start with
. Pay attention to the attributes of HTML tags such as onclick and onchange. In JSX, onClick must be written in small camel case for naming to be effective.
In order to support JSX, you need to set javascript in WebStrom to JSX Harmony
React features:
1. Declarative design −React adopts a declarative paradigm, which can easily describe applications.
2. Efficient −React minimizes interaction with the DOM by simulating the DOM.
3. Flexible −React works well with known libraries or frameworks.
4.JSX − JSX is an extension of JavaScript syntax. JSX is not required for React development, but it is recommended.
5. Components - Building components through React makes it easier to reuse code and can be well applied in the development of large projects.
6. One-way response data flow − React implements one-way response data flow, thereby reducing duplicate code, which is why it is simpler than traditional data binding.
Because React is a JAVASCRIPT library used to build user interfaces, so first reference three js files, or you can Download it and reference it locally.
82009ba41b15f51705420ba40a538ee32cacc6d41bbb37262a98f745aa00fbf0 abfd62c5aa399bcfd7beb6308b55f1ce2cacc6d41bbb37262a98f745aa00fbf0 726647b8f4ec8af879e4a70f56dccdd12cacc6d41bbb37262a98f745aa00fbf0 9fd01892b579bba0c343404bcccd70fb 93f0f5c25f18dab9d176bd4f6de5d30e a80eb7cbb6fff8b0ff70bae37074b813 b2386ffb911b14667cb8f0f91ea547a7Title6e916e0f7d1e588d4f442bf645aedb2f 7682b9a3d08eed176a4308470f476c472cacc6d41bbb37262a98f745aa00fbf0 a8b6889a40c49db76102831f2183fd612cacc6d41bbb37262a98f745aa00fbf0 a8a634993edeeb9b1cd75e43ca00a5762cacc6d41bbb37262a98f745aa00fbf0 d87a8a64ee2bbb5de77d915dd08e24cd2cacc6d41bbb37262a98f745aa00fbf0 9c3bca370b5104690d9ef395f2c5f8d1 6c04bd5ca3fcae76e30b72ad730ca86d 803c97d8346ca6b8da89108d684ef6bb94b3e26ee717c64999d7867364b1b4a3 36cc49f0c466276486e50c850b7e4956 73a6ac4ed44ffec12cee46588e518a5e
react.min.js - React 的核心库 react-dom.min.js - 提供与 DOM 相关的功能 babel.min.js - Babel 可以将 ES6 代码转为 ES5 代码,这样我们就能在目前不支持 ES6 浏览器上执 行 React 代码。Babel 内嵌了对 JSX 的支持。通过将 Babel 和 babel-sublime 包(package) 一同使用可以让源码的语法渲染上升到一个全新的水平。
2.1 Method 1:
class MyTextView extends React.Component{ render(){ return e388a4556c0f65e1904146cc1a846beehello react94b3e26ee717c64999d7867364b1b4a3; } } //组件渲染到DOM中 ReactDOM.render(a4d1e0120195a60a9692a1c120c95952054fb429a2d2fb32ca4dca4607c66fb6,document.body);
must inherit React.Component
render( ) The rendering function is a required
>A required method to create a virtual DOM. This method has special rules:
1. It can only pass this.props and this. state access data
2. Can return null, false or any React component
3. Cannot change the state of the component
4. Cannot modify the output of the DOM
var MyTexTiew2 = React.createClass( { render:function () { return e388a4556c0f65e1904146cc1a846beehi react94b3e26ee717c64999d7867364b1b4a3; } } ); ReactDOM.render(53ce64b5f535a04a91cafbb0f9bb455339edcc07d374c8b1e032d2a6a06eb03c,document.body);
State is mainly used to update the interface. The component's State property is initialized in the life cycle function getInitialState. When calling this.setState of the component When the state is changed, the component will be re-rendered and refreshed.React component life cycleInstantiationFirst instantiationProps are mainly used to transfer data between components, that is, the pname attribute of the label can be obtained through this.props.pname in MyText
var MyTextView = React.createClass( { render:function(){ return e388a4556c0f65e1904146cc1a846beecontent:{this.props.contentText}94b3e26ee717c64999d7867364b1b4a3; } } ); var MyViewGroup = React.createClass({ getInitialState:function () { //生命周期函数,返回一个对象 return {text:""}; }, handleChange:function (e) { //改变组件的state属性 this.setState({text:e.target.value}); }, render:function () { return e388a4556c0f65e1904146cc1a846bee f2023886b1e331e67cfc1ccb1ec0f1d4 //注意属性onChange大写 981869dd17be54337e2c38ef89b21efd96ac42c95369ad8c9dbbd274b6bcc83a 94b3e26ee717c64999d7867364b1b4a3; } }); ReactDOM.render(a9ad2d7d068754741782938dda7cf021,document.getElementById("content"));
getDefaultProps
getInitialState
componentWillMount
render
componentDidMount
getInitialState
componentWillMount
render
componentDidMount
State changes when the component already exists
componentWillReceiveProps
shouldComponentUpdate
componentWillUpdate
render
componentDidUpdate
The componentWillUnmount
Acts on the component class and is only called once. The returned object is used to set the default props. The reference value will be shared in the instance.
Acts on the instance of the component. It is called once when the instance is created to initialize the state of each instance. This.props can be accessed at this time.
Called before completing the first rendering, the state of the component can still be modified at this time. (If you want to see more, go to the PHP Chinese website
React Reference Manual column to learn)
4.render
必选的方法,创建虚拟DOM,该方法具有特殊的规则:
只能通过this.props和this.state访问数据
可以返回null、false或任何React组件
只能出现一个顶级组件(不能返回数组)
不能改变组件的状态
不能修改DOM的输出
5.componentDidMount
真实的DOM被渲染出来后调用,在该方法中可通过this.getDOMNode()访问到真实的DOM元素。此时已可以使用其他类库来操作这个DOM。
在服务端中,该方法不会被调用。
6.componentWillReceiveProps
组件接收到新的props时调用,并将其作为参数nextProps使用,此时可以更改组件props及state。
componentWillReceiveProps: function(nextProps) { if (nextProps.bool) { this.setState({ bool: true }); } }
7.shouldComponentUpdate
组件是否应当渲染新的props或state,返回false表示跳过后续的生命周期方法,通常不需要使用以避免出现bug。在出现应用的瓶颈时,可通过该方法进行适当的优化。
在首次渲染期间或者调用了forceUpdate方法后,该方法不会被调用
8.componentWillUpdate
接收到新的props或者state后,进行渲染之前调用,此时不允许更新props或state。
9.componentDidUpdate
完成渲染新的props或者state后调用,此时可以访问到新的DOM元素。
10.componentWillUnmount
组件被移除之前被调用,可以用于做一些清理工作,在componentDidMount方法中添加的所有任务都需要在该方法中撤销,比如创建的定时器或添加的事件监听器。
通过集成extends React.Component 给组件初始化不会执行getDefaultProps、getInitialState
只有通过以下方式给组件初始化state和props
1、state的初始化,通过构造函数
//在构造函数中对状态赋初始值 constructor(props){ super(props);//第一步,这是必须的 //不能调用state this.state = {//第二步,赋初始值 time:0, on:false, log:[] //数组 }; }
2、props的初始化
class Button extends React.Component{ //静态属性,给属性赋默认值 static defaultProps = { onClick : null, className : '', text : '默认' }; render(){ return ef4946c73039c083200512dcbcc5f2b5{this.props.text}65281c5ac262bf6d81768915a4a77ac0; }
本篇文章到这就结束了(想看更多就到PHP中文网React使用手册栏目中学习),有问题的可以在下方留言提问。
The above is the detailed content of What are the basic syntaxes of React? Introduction to the basic syntax of react (with examples). For more information, please follow other related articles on the PHP Chinese website!