This article mainly introduces the application code analysis of es6 in react. It is very good and has reference value. Friends in need can refer to it. I hope it can help everyone better understand and master the application of es6 in react.
Whether it is React or React-native, Facebook officially recommends using ES6 syntax. If you have not used it in a project, you will encounter some problems if you suddenly switch to it. If you have not had time to systematically learn ES6, then Note that some common writing methods are enough for the time being, which will bring great convenience to our development. You will experience the extremely simple syntax of ES6. Let me introduce to you the application of es6 in react. The specific content is as follows:
import React,{Component} from 'react'; class RepeatArrayextends Component{ constructor() { super(); } render(){ const names = ['Alice', 'Emily', 'Kate']; return ( <p> { names.map((name) =>{return </p><p>Hello, {name}!</p>;} ) } ); } } export default RepeatArray;
2. Implementation of ol and li
import React,{Component} from 'react'; class RepeatLiextends Component{ render(){ return ( <ol> { this.props.children.map((child)=>{return <li>{child}</li>}) } </ol> ); } } class RepeatArray extends Component{ constructor() { super(); } render(){ return ( <p> <repeatli> <span>hello</span> <span>world</span> </repeatli> </p> ); } } export default RepeatArray;
3. Obtaining data from the server
import React,{Component} from 'react'; class UserGistextends Component{ constructor(){ super(); this.state={ username:'', lastGistUrl:'' } } componentWillMount(){ $.get(this.props.source, function(result){ var lastGist = result[0]; //if (this.isMounted()) { this.setState({ username: lastGist.owner.login, lastGistUrl: lastGist.html_url }); //} }.bind(this)); } render(){ return( <p> {this.state.username} .. <a>here</a> </p> ); } } class RepeatArrayextends Component{ constructor() { super(); } render(){ return ( <p> <usergist></usergist> </p> ); } } export default RepeatArray;
4. Initialization STATE
class Videoextends React.Component{ constructor(props){ super(props); this.state = { loopsRemaining: this.props.maxLoops, }; } }
5. Destructuring and expansion operators
It is more convenient to pass a batch of attributes to sub-components. The following example passes all attributes except className to the p tag
class AutoloadingPostsGridextends React.Component{ render() { var { className, ...others, // contains all properties of this.props except for className } = this.props; return ( <p> <postsgrid></postsgrid> <button>Load more</button> </p> ); } }
The most common problem when using react development is that it is troublesome when the parent component wants to pass many attributes to the child component
class MyComponentextends React.Component{ //假设MyComponent已经有了name和age属性 render(){ return ( <subcomponent></subcomponent> ) } }
Use extensions The operator can be very simple
class MyComponentextends React.Component{ //假设MyComponent已经有了name和age属性 render(){ return ( <subcomponent></subcomponent> ) } }
The above method is to pass all the properties of the parent component. What if I don’t need to pass some of the properties? It’s also very simple
class MyComponentextends React.Component{ //假设MyComponent有很多属性,而name属性不需要传递给子组件 var {name,...MyProps}=this.props; render(){ return ( <subcomponent></subcomponent> ) } }
The most commonly used scenario for the above method is that the class attribute of the parent component needs to be extracted separately as the class of a certain element, and other attributes need to be passed to the child component
6. Create components
import React,{Component} from "react"; class MyComponentextends Component{ //组件内部代码 }
7. State/Props/PropTypes
es6 allows props and propTypes to be initialized outside the class as static properties
class MyComponentextends React.Component{} MyComponent.defaultProps={ name:"SunnyChuan", age:22 }; MyComponent.propTypes={ name:React.PropTypes.string.isRequired, age:React.PropTypes.number.isRequired };
es7 supports direct use in the class Variable expression
class MyComponentextends React.Component{ static defaultProps={ name:"SunnyChuan", age:22 } static propTypes={ name:React.PropTypes.string.isRequired, age:React.PropTypes.number.isRequired } }
state is different from the first two, it is not static
class MyComponentextends React.Component{ static defaultProps={ name:"SunnyChuan", age:22 } state={ isMarried:false } static propTypes={ name:React.PropTypes.string.isRequired, age:React.PropTypes.number.isRequired } }
7. When you build a general container, extended attributes will be very useful
function App1(){ return <greetingfirstname></greetingfirstname>; } function App2() { const props = {firstName: 'Ben', lastName: 'Hector'}; return <greeting></greeting>; }
8 , Use the calculated properties of es6 instead
this.setState({ [name]:value }) //代替 var partialState = {}; partialState[name] = value; this.setState(partialState);
Related recommendations:
Detailed explanation of the correct posture of using Echarts in React components
Detailed example of React passing the class name to the child component through the parent component
What are the ways to write components in React
The above is the detailed content of Detailed examples of the application of es6 in react. For more information, please follow other related articles on the PHP Chinese website!

在react中,canvas用于绘制各种图表、动画等;可以利用“react-konva”插件使用canvas,该插件是一个canvas第三方库,用于使用React操作canvas绘制复杂的画布图形,并提供了元素的事件机制和拖放操作的支持。

在react中,antd是基于Ant Design的React UI组件库,主要用于研发企业级中后台产品;dva是一个基于redux和“redux-saga”的数据流方案,内置了“react-router”和fetch,可理解为应用框架。

React不是双向数据流,而是单向数据流。单向数据流是指数据在某个节点被改动后,只会影响一个方向上的其他节点;React中的表现就是数据主要通过props从父节点传递到子节点,若父级的某个props改变了,React会重渲染所有子节点。

因为在react中需要利用到webpack,而webpack依赖nodejs;webpack是一个模块打包机,在执行打包压缩的时候是依赖nodejs的,没有nodejs就不能使用webpack,所以react需要使用nodejs。

react是组件化开发;组件化是React的核心思想,可以开发出一个个独立可复用的小组件来构造应用,任何的应用都会被抽象成一颗组件树,组件化开发也就是将一个页面拆分成一个个小的功能模块,每个功能完成自己这部分独立功能。

react和reactdom的区别是:ReactDom只做和浏览器或DOM相关的操作,例如“ReactDOM.findDOMNode()”操作;而react负责除浏览器和DOM以外的相关操作,ReactDom是React的一部分。

在react中,forceupdate()用于强制使组件跳过shouldComponentUpdate(),直接调用render(),可以触发组件的正常生命周期方法,语法为“component.forceUpdate(callback)”。

react中没有双向绑定;react的设计思想就是单向数据流,没有双向绑定的概念;react是view层,单项数据流只能由父组件通过props将数据传递给子组件,满足了view层渲染的要求并且更易测试与控制,所以在react中没有双向绑定。


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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

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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.