国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Web Front-end Front-end Q&A Is react.js written in es6?

Is react.js written in es6?

Oct 20, 2022 pm 06:24 PM
react javascript es6

react.js is written in es6. It can be translated into ES5 using Babel, or it can be translated into JavaScript’s JSX using Babel. Because React’s design idea is extremely unique, it is a revolutionary innovation, outstanding performance, and code logic. But very simple. The syntax for creating components using ES6 is more concise. This syntax avoids too much React boilerplate code and uses more pure JavaScript syntax, which is more in line with JavaScript syntax habits.

Is react.js written in es6?

The operating environment of this tutorial: windows7 system, ECMAScript 6&&react17 version, Dell G3 computer.

ReactJS is the most popular front-end library for building views. ReactJS is written in ES6 and can be translated to ES5 with Babel, or JSX of JavaScript. Because the design idea of ??React is extremely unique, it is a revolutionary innovation, has outstanding performance, and the code logic is very simple. Therefore, more and more people are starting to pay attention to and use it, and it may be the mainstream tool for web development in the future.

Comparison of React writing methods using ES6 and ES5

Creating components

Components created by ES6 class The syntax is more concise and more consistent with JavaScript. Internal methods do not need to use the function keyword.

React.createClass

import React from 'react';

const MyComponent = React.createClass({
  render: function() {
    return (
      <div>以前的方式創(chuàng)建的組件</div>
    );
  }
});

export default MyComponent;

React.Component(ES6)

import React,{ Component } from &#39;react&#39;;

class MyComponent extends Component {
  render() {
    return (
      <div>ES6方式創(chuàng)建的組件</div>
    );
  }
}

export default MyComponent;

props propTypes and getDefaultProps

  • To create a component using React.Component, you need to pass props to React.Component by calling super() in the constructor. In addition, props must be immutable after react 0.13.

  • Since the component is created using ES6 class syntax, only methods are allowed to be defined internally, but not attributes. Class attributes can only be defined outside the class. So propTypes should be written outside the component.

  • For the previous getDefaultProps method, since props are immutable, it is now defined as a property. Like propTypes, it must be defined outside the class.

React.createClass

import React from &#39;react&#39;;

const MyComponent = React.createClass({
  propTypes: {
    nameProp: React.PropTypes.string
  },
  getDefaultProps() {
    return {
      nameProp: &#39;&#39;
    };
  },
  render: function() {
    return (
      <div>以前的方式創(chuàng)建的組件</div>
    );
  }
});

export default MyComponent;

React.Component(ES6)

import React,{ Component } from &#39;react&#39;;

class MyComponent extends Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <div>ES6方式創(chuàng)建的組件</div>
    );
  }
}

MyComponent.propTypes = {
  nameProp: React.PropTypes.string
};
MyComponent.defaultProps = {
  nameProp: &#39;&#39;
};

export default MyComponent;

State

Use ES6 Class syntax creates components, and the work of initializing state must be completed in the constructor. There is no need to call the getInitialState method again. This syntax is more in line with JavaScript language habits.

React.createClass

import React from &#39;react&#39;;const MyComponent = React.createClass({
  getInitialState: function() {
    return { data: [] };
  },

  render: function() {
    return (
      <div>以前的方式創(chuàng)建的組件</div>
    );
  }});export default MyComponent;

React.Component(ES6)

import React,{ Component } from &#39;react&#39;;class MyComponent extends Component {
  constructor(props) {
    super(props);
    this.state = { data: [] };
  }

  render() {
    return (
      <div>ES6方式創(chuàng)建的組件</div>
    );
  }}export default MyComponent;

this

Use ES6 class syntax to create components, class Methods in do not automatically bind this to the instance. You must use .bind(this) or arrow function => for manual binding.

React.createClass

import React from &#39;react&#39;;

const MyComponent = React.createClass({
  handleClick: function() {
    console.log(this);
  },
  render: function() {
    return (
      <div onClick={this.handleClick}>以前的方式創(chuàng)建的組件</div>
    );
  }
});

export default MyComponent;

React.Component(ES6)

import React,{ Component } from &#39;react&#39;;

class MyComponent extends Component {
  handleClick() {
    console.log(this);
  }

  render() {
    return (
      <div onClick={this.handleClick.bind(this)}>ES6方式創(chuàng)建的組件</div>
    );
  }
}

export default MyComponent;

You can also write the binding method into the constructor:

import React,{ Component } from &#39;react&#39;;

class MyComponent extends Component {
  constructor(props) {
    super(props);
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
    console.log(this);
  }

  render() {
    return (
      <div onClick={this.handleClick}>ES6方式創(chuàng)建的組件</div>
    );
  }
}

export default MyComponent;

Or use arrows Function =>:

mport React,{ Component } from &#39;react&#39;;

class MyComponent extends Component {
  handleClick = () => {
    console.log(this);
  }

  render() {
    return (
      <div onClick={this.handleClick}>ES6方式創(chuàng)建的組件</div>
    );
  }
}

export default MyComponent;

Mixins

Using ES6 syntax to create components does not support React mixins. If you must use React mixins You can only use the React.createClass method to create components.

import React,{ Component } from &#39;react&#39;;

var SetIntervalMixin = {
  doSomething: function() {
    console.log(&#39;do somethis...&#39;);
  },
};
const Contacts = React.createClass({
  mixins: [SetIntervalMixin],

  handleClick() {
    this.doSomething(); //使用mixin
  },
  render() {
    return (
      <div onClick={this.handleClick}></div>
    );
  }
});

export default Contacts;

Summary

In general, the syntax for creating components using ES6 is more concise. This syntax avoids too much React boilerplate code, and uses more pure javascript syntax, which is more Comply with javascript syntax habits. React officially does not mandate which syntax to use, you can choose a reasonable one according to your needs.

【Related recommendations: javascript video tutorial, programming video

The above is the detailed content of Is react.js written in es6?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Integration of Java framework and front-end React framework Integration of Java framework and front-end React framework Jun 01, 2024 pm 03:16 PM

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.

Vue.js vs. React: Project-Specific Considerations Vue.js vs. React: Project-Specific Considerations Apr 09, 2025 am 12:01 AM

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.

React vs. Vue: Which Framework Does Netflix Use? React vs. Vue: Which Framework Does Netflix Use? Apr 14, 2025 am 12:19 AM

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

React's Role in HTML: Enhancing User Experience React's Role in HTML: Enhancing User Experience Apr 09, 2025 am 12:11 AM

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.

React's Ecosystem: Libraries, Tools, and Best Practices React's Ecosystem: Libraries, Tools, and Best Practices Apr 18, 2025 am 12:23 AM

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's Frontend: Examples and Applications of React (or Vue) Netflix's Frontend: Examples and Applications of React (or Vue) Apr 16, 2025 am 12:08 AM

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.

React: The Power of a JavaScript Library for Web Development React: The Power of a JavaScript Library for Web Development Apr 18, 2025 am 12:25 AM

React is a JavaScript library developed by Meta for building user interfaces, with its core being component development and virtual DOM technology. 1. Component and state management: React manages state through components (functions or classes) and Hooks (such as useState), improving code reusability and maintenance. 2. Virtual DOM and performance optimization: Through virtual DOM, React efficiently updates the real DOM to improve performance. 3. Life cycle and Hooks: Hooks (such as useEffect) allow function components to manage life cycles and perform side-effect operations. 4. Usage example: From basic HelloWorld components to advanced global state management (useContext and

The Future of React: Trends and Innovations in Web Development The Future of React: Trends and Innovations in Web Development Apr 19, 2025 am 12:22 AM

React's future will focus on the ultimate in component development, performance optimization and deep integration with other technology stacks. 1) React will further simplify the creation and management of components and promote the ultimate in component development. 2) Performance optimization will become the focus, especially in large applications. 3) React will be deeply integrated with technologies such as GraphQL and TypeScript to improve the development experience.

See all articles