Home Web Front-end JS Tutorial React.js introductory example tutorial: 5 ways to create hello world

React.js introductory example tutorial: 5 ways to create hello world

Jan 21, 2017 am 10:59 AM

1. Introduction to ReactJS

React is a very popular front-end development framework recently. React originated as an internal project at Facebook. Because the company was dissatisfied with all the JavaScript MVC frameworks on the market, it decided to write its own to build the Instagram website. After making it, I found that this set of things is very useful, so it was open sourced in May 2013. Because React's design idea is extremely unique, it is a revolutionary innovation, has outstanding performance, and the code logic is very simple. Therefore, more and more people are beginning to pay attention to and use it, thinking that it may be the mainstream tool for web development in the future.

ReactJS official website address: http://facebook.github.io/react/

Github address: https://github.com/facebook/react

ReactJS Chinese address: http://reactjs.cn/react/docs/getting-started.html

What is React?

React is a JS library developed by outstanding programmers working at Facebook for developing user interaction interfaces. Its source code is maintained by outstanding programmers from Facebook and the community, so there is a very strong technical team behind it to provide technical support. React brings many new things, such as componentization, JSX, virtual DOM, etc. The virtual DOM it provides allows us to render components very quickly, freeing us from the heavy work of frequently manipulating the DOM. Anyone who knows React knows that the work it does is more focused on the V layer in MVC. Combined with others such as Flux, you can easily build powerful applications.

2. Features of ReactJS

1. Virtual DOM

Through the DOM diff algorithm, only the differentiated parts will be updated. No need to Render the entire page to improve efficiency

2. Componentization

Divide the page into several components, which contain logical structures and styles

The component only contains its own logic, and it can be predicted when updating the component, which is conducive to maintenance

The page is split into multiple components and can be reused

3, one-way Data flow

Data is passed from the top-level component to the sub-component

Data is controllable

3. Getting started with React writing Hello, world First, let’s understand what JSX is. One of the core mechanisms of React is virtual DOM: virtual DOM elements that can be created in memory. React uses virtual DOM to reduce operations on the actual DOM and improve performance. Similar to the real native DOM, virtual DOM can also be created through JavaScript, for example:

var child1 = React.createElement('li', null, 'First Text Content');
var child2 = React.createElement('li', null, 'Second Text Content');
var root2 = React.createElement('ul', { className: 'my-list' }, child1, child2);
React.render(
<div>{root2}</div>,
document.getElementById(&#39;container5&#39;)
);
Copy after login

Using such a mechanism, we can use JavaScript to build a complete interface DOM tree, just as we can use JavaScript to create a real DOM . But the readability of such code is not good, so React invented JSX, using our familiar HTML syntax to create a virtual DOM:

var root =(
<ul className="my-list">
<li>First Text Content</li>
<li>Second Text Content</li>
</ul>
);
React.render(
<div>{root}</div>,
document.getElementById(&#39;container6&#39;)
);
Copy after login

4. 5 ways to get started with writing Hello, world in React

The 1st way

<div id="example1"></div>
<script type="text/jsx">
React.render( //直接html
<h1 className="classN1" >1 hellow 直接 html world </h1>,
document.getElementById(&#39;example1&#39;)
);
</script>
Copy after login

The 2nd way

<div id="example2"></div>
<script type="text/jsx">
React.render( //直接创建元素
React.createElement(&#39;h1&#39;, {className:&#39;classN2&#39;}, &#39;2 Hello, 直接创建元素 world!&#39;),
document.getElementById(&#39;example2&#39;)
);
</script>
Copy after login

The 3rd way

<div id="example3"></div>
<script type="text/jsx">
var CreateEl=React.createClass({
render:function(){
// return <h1>hellow 组件 创建 html world </h1> //有无括号均可
return (<h1 className=&#39;classN3&#39; >3 hellow 组件 创建 html world </h1>);
}
});
React.render( //组件方式创建元素
<CreateEl/>,
//或者双括号 <CreateEl></CreateEl>
document.getElementById(&#39;example3&#39;)
);
</script>
Copy after login

The 4th way

<div id="example4"></div>
<script type="text/jsx">
var JsxCreateEl=React.createClass({ // 直接 jsx 方式 创建
render:function(){
return (
React.createElement(&#39;h1&#39;, {className: "classN4"},"4 Hello, 直接 jsx 方式 创建 world! ")
)
}
});
React.render( //组件方式创建元素
React.createElement(JsxCreateEl, null),
document.getElementById(&#39;example4&#39;)
);
</script>
Copy after login

The 5th way Method

<div id="example5"></div>
<script type="text/jsx">
var Hello=React.createClass({ // 模板 Hello
render:function(){
return (<span>{this.props.data}</span>)
}
});
var World=React.createClass({ // 模板 world
render:function(){
return (<span> 和 World 模板拼接</span>)
}
});
React.render( // 2个 模板 组件方式创建元素
<h1 className="classN5" >
<Hello data=&#39;5 hello&#39; />
<World />
</h1>,
document.getElementById(&#39;example5&#39;)
);
</script>
Copy after login

5. The above result picture

Attach the code: React.js入门实例教程之创建hello world 的5种方式

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<meta name="viewport" content="initial-scale=1.0,user-scalable=no,maximum-scale=1,width=device-width" />
</head>
<body>
<style>
*{ margin:0; padding:0;}
body{ background:#333;}
#box{ background:url(loveImg/QioA-fxehfqi8208393.jpg) no-repeat center top; width:550px; border:8px solid #fff; -webkit-box-sizing:border-box; margin:50px auto;}
#example1,#example2,#example3,#example4,#example5{ margin:20px auto; width:100%; background:rgba(255,255,255,.3); padding:5px 10px; font-size:13px; color:#f1f1f1;-webkit-box-sizing:border-box; }
</style>
<div id="box">
<div id="example1"></div>
<script type="text/jsx">
React.render( //直接html
<h1 className="classN1" >1 hellow 直接 html world </h1>, 
document.getElementById(&#39;example1&#39;)
);
</script>
<div id="example2"></div>
<script type="text/jsx">
React.render( //直接创建元素
React.createElement(&#39;h1&#39;, {className:&#39;classN2&#39;}, &#39;2 Hello, 直接创建元素 world!&#39;), 
document.getElementById(&#39;example2&#39;)
);
</script>
<div id="example3"></div>
<script type="text/jsx">
var CreateEl=React.createClass({
render:function(){
// return <h1>hellow 组件 创建 html world </h1> //有无括号均可 
return (<h1 className=&#39;classN3&#39; >3 hellow 组件 创建 html world </h1>);
}
});
React.render( //组件方式创建元素
<CreateEl/>, 
//或者双括号 <CreateEl></CreateEl>
document.getElementById(&#39;example3&#39;)
);
</script>
<div id="example4"></div> 
<script type="text/jsx">
var JsxCreateEl=React.createClass({ // 直接 jsx 方式 创建
render:function(){
return (
React.createElement(&#39;h1&#39;, {className: "classN4"},"4 Hello, 直接 jsx 方式 创建 world! ")
)
}
});
React.render( //组件方式创建元素
React.createElement(JsxCreateEl, null), 
document.getElementById(&#39;example4&#39;)
);
</script>
<div id="example5"></div> 
<script type="text/jsx">
var Hello=React.createClass({ // 模板 Hello 
render:function(){
return (<span>{this.props.data}</span>)
}
});
var World=React.createClass({ // 模板 world 
render:function(){
return (<span> 和 World 模板拼接</span>)
}
});
React.render( // 2个 模板 组件方式创建元素
<h1 className="classN5" >
<Hello data=&#39;5 hello&#39; />
<World />
</h1>, 
document.getElementById(&#39;example5&#39;)
);
</script>
</div>
<script src="build/react.min.js"></script>
<script src="build/JSXTransformer.js"></script>
</body>
</html>
Copy after login

Let me add some knowledge to you:

React terminology

React Elements

Represents the JavaScript object of HTML elements. These JavaScript objects are finally compiled into corresponding HTML elements.

Components

Encapsulates React Elements and contains one or more React Elements. Components are used to create reusable UI modules, such as paging, sidebar navigation, etc.

JSX

JSX is a JavaScript syntax extension defined by React, similar to XML. JSX is optional, we can use JavaScript to write React applications, but JSX provides a simpler way to write React applications.

Virtual DOM

Virtual DOM is a JavaScript object that simulates a DOM tree. React uses Virtual DOM to render the UI, while monitoring data changes on the Virtual DOM and automatically migrating these changes to the UI.

For more React.js introductory example tutorials: 5 ways to create hello world and related articles, please pay attention to 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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

See all articles