Reactjs.net Development

Source: Internet
Author: User

Reactjs is also often referred to as "React", a novice who has just appeared in the game. It was created by Facebook and was first released in 2013. Facebook thinks react can be a angular alternative to dealing with spa issues, so if you think angular and react are rivals, then you're right. However, the biggest difference between react and angular is that it is a more efficient, higher performance, faster class library. Shows the time required to use react, Angular, Knockout (another class library, not discussed in this article), and pure JavaScript to render a list of 1000 content in the DOM:

Source: The Dapper Developer

If your application values performance, then react is the right choice. As a result, this year's ANGULARJS seminar also mentions that the two can be combined to make the whole page performance a step further.

Interested people can refer to the following demo Code:https://github.com/djsmith42/angular_react_directive_example

About Reactjs. NET

It is dedicated to. NET platform developers, so that we can not only render the page on the front end, but also on the server side to render the page. The development Reactjs can use the General JS Library, but also can be developed by the way of XML-like JSX writing. The official website http://reactjs.net/, can already support ASP. 5!

We refer to the Getting Started tutorial (http://reactjs.cn/react/docs/getting-started.html), first we create an empty ASP. NET MVC 4 project, you can install the Reactjs.net component through NuGet:

Create the first component Commentbox:

@{

Layout = null;

}

<title>hello react</title>

<body>

<div id= "Content" ></div>

<script src= "@Url. Content (" ~/scripts/react.js ")" ></script>

<script src= "@Url. Content (" ~/scripts/tutorial.jsx ")" ></script>

</body>

Add the following code to TUTORIAL.JSX:

var Commentbox = React.createclass ({

Render:function () {

Return (

<div classname= "Commentbox" >

Hello, world!. I am a commentbox.

</div>

);

}

});

React.render (

<commentbox/>

document.getElementById (' content ')

);

Each interface that wants to render is manipulated as a component, which is the generative syntax of a component, where the component is commentbox. After being rendered in the content, it is recommended that you place the Jsx file of the generated object behind the tag that you want to render.

If you want to bind data, you can also write this:

var Commentbox = React.createclass ({

Render:function () {

Return (

<div classname= "Commentbox" >

<commentlist Data={this.props.data}/>

<br/>

</div>

);

}

});

var data = [

{Author: "Daniel Lo AAA", Text: "Hello reactjs.net world!"},

{Author: "Pete Hunt BBB", Text: "This is one comment"},

{Author: "Jordan walke CCC", Text: "This is *another* comment"}

];

var commentlist = React.createclass ({

Render:function () {

var commentnodes = This.props.data.map (function (FFF) {

Return (

<comment author={fff. Author}>

{FFF. Text}

</Comment>

);

});

Return (

<div classname= "Commentlist" >

{Commentnodes}

</div>

);

}

});

var Comment = React.createclass ({

Render:function () {

Return (

<div classname= "comment" >

<H2 classname= "Commentauthor" >

{This.props.author}

{This.props.children}

</div>

);

}

});

React.render (

<commentbox Data={data}/>

document.getElementById (' content ')

);

The results are as follows:

This section is mainly to put data this dataset into the object Commentbox, in the Ccommentbox object also contains the object commentlist, so, must produce commenlist this object, and in this object processing data, In commentlist, the rendering definition for each piece of data is defined as:

var commentnodes = This.props.data.map (function (FFF) {

Return (

<comment author={fff. Author}>

{FFF. Text}

</Comment>

);

Finally, the object is generated by comment and the style of each piece of data is defined within the object. You can present each data in a list way. And in the most important point of using JSX, be sure to include the file at the very beginning

/** @jsx React.dom */

Although, it is written in jsx way, but in the end will be compiled into a JS file, if not joined this line, there will be no action, and must be in the MVC view of

<script src= "@Url. Content (" ~/scripts/react.js ")" ></script>

If you want to use the server to pull the data, you must take into account the react State, when the page initialization and there is no data, but will first render the page to call the MVC controller to take out the data, when the data will be changed, and then to update the data.

Public ActionResult Reactjs ()

{

ilist<commentmodel> _comments = new list<commentmodel>

{

New Commentmodel

{

Author = "Daniel Lo Nigro",

Text = "Hello reactjs.net world!"

},

New Commentmodel

{

Author = "Pete Hunt",

Text = "This is one comment"

},

New Commentmodel

{

Author = "Jordan Walke",

Text = "This is *another* comment"

},

};

Return Json (_comments, jsonrequestbehavior.allowget);

}

JSX can apply the above script to make partial changes, get the data to the local change to get, the complete code is as follows:

/** @jsx React.dom */

var Commentbox = React.createclass ({

Getinitialstate:function () {

return {data: []};

},

Componentwillmount:function () {

var xhr = new XMLHttpRequest ();

Xhr.open (' Get ', This.props.url, true);

Xhr.onload = function () {

var data = Json.parse (Xhr.responsetext);

This.setstate ({data:data});

}.bind (this);

Xhr.send ();

},

Render:function () {

Return (

<div classname= "Commentbox" >

<commentlist Data={this.state.data}/>

<br/>

</div>

);

}

});

var commentlist = React.createclass ({

Render:function () {

var commentnodes = This.props.data.map (function (FFF) {

Return (

<comment author={fff. Author}>

{FFF. Text}

</Comment>

);

});

Return (

<div classname= "Commentlist" >

{Commentnodes}

</div>

);

}

});

var Comment = React.createclass ({

Render:function () {

Return (

<div classname= "comment" >

<H2 classname= "Commentauthor" >

{This.props.author}

{This.props.children}

</div>

);

}

});

React.render (

<commentbox url= "/home/reactjs"/>

document.getElementById (' content ')

);

Above is a brief demonstration of the use of reactjs.net, further please refer to the official website http://reactjs.net/documentation, follow-up continue to learn Reactjs.

Sample code Download: Http://files.cnblogs.com/files/shanyou/ReactDemo.zip

React Chinese web

React Getting Started Tutorial

Disruptive front-end UI development framework: React

React (i): React's philosophy of design-the beauty of simplicity

React Native Exploration (i): background, planning and risk

Reactjs.net Development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.