[Fuente: http://facebook.github.io/react/index.html]
JUST THE UI
Lots of people use React as the V in MVC. Since React makes no assumptions about the rest of your technology stack, it’s easy to try it out on a small feature in an existing project.
VIRTUAL DOM
React uses a virtual DOM diff implementation for ultra-high performance. It can also render on the server using Node.js — no heavy browser DOM required.
DATA FLOW
React implements one-way reactive data flow which reduces boilerplate and is easier to reason about than traditional data binding.
Getting Started
JSFiddle
The easiest way to start hacking on React is using the following JSFiddle Hello World examples:
Starter Kit
Download the starter kit to get started.
In the root directory of the starter kit, create a helloworld.html
with the following contents.
<!DOCTYPE html>
<html>
<head>
<script src="build/react.js"></script>
<script src="build/JSXTransformer.js"></script>
</head>
<body>
<div id="example"></div>
<script type="text/jsx">
React.render(
<h1>Hello, world!</h1>,
document.getElementById('example')
);
</script>
</body>
</html>
The XML syntax inside of JavaScript is called JSX; check out the JSX syntax to learn more about it. In order to translate it to vanilla JavaScript we use <script type="text/jsx">
and include JSXTransformer.js
to actually perform the transformation in the browser.
Separate File
Your React JSX code can live in a separate file. Create the following src/helloworld.js
.
React.render(
<h1>Hello, world!</h1>,
document.getElementById('example')
);
Then reference it from helloworld.html
:
<script type="text/jsx" src="src/helloworld.js"></script>
Offline Transform
First install the command-line tools (requires npm):
npm install -g react-tools
Then, translate your src/helloworld.js
file to plain JavaScript:
jsx --watch src/ build/
The file build/helloworld.js
is autogenerated whenever you make a change.
React.render(
React.createElement('h1', null, 'Hello, world!'),
document.getElementById('example')
);
Update your HTML file as below:
<!DOCTYPE html>
<html>
<head>
<title>Hello React!</title>
<script src="build/react.js"></script>
<!-- No need for JSXTransformer! -->
</head>
<body>
<div id="example"></div>
<script src="build/helloworld.js"></script>
</body>
</html>
Want CommonJS?
If you want to use React with browserify, webpack, or another CommonJS-compatible module system, just use the react
npm package. In addition, the jsx
build tool can be integrated into most packaging systems (not just CommonJS) quite easily.
Next Steps
Check out the tutorial and the other examples in the starter kit’s examples
directory to learn more.
We also have a wiki where the community contributes with workflows, UI-components, routing, data management etc.
Good luck, and welcome!
A Simple Component
React components implement a render()
method that takes input data and returns what to display. This example uses an XML-like syntax called JSX. Input data that is passed into the component can be accessed by render()
via this.props
.
JSX is optional and not required to use React. Try clicking on “Compiled JS” to see the raw JavaScript code produced by the JSX compiler.
This code displays “Hello John”:
var HelloMessage = React.createClass({ render: function() { return <div>Hello {this.props.name}</div>; } }); React.render(<HelloMessage name="John" />, mountNode);
A Stateful Component
In addition to taking input data (accessed via this.props
), a component can maintain internal state data (accessed via this.state
). When a component’s state data changes, the rendered markup will be updated by re-invoking render()
.
var Timer = React.createClass({ getInitialState: function() { return {secondsElapsed: 0}; }, tick: function() { this.setState({secondsElapsed: this.state.secondsElapsed + 1}); }, componentDidMount: function() { this.interval = setInterval(this.tick, 1000); }, componentWillUnmount: function() { clearInterval(this.interval); }, render: function() { return ( <div>Seconds Elapsed: {this.state.secondsElapsed}</div> ); } }); React.render(<Timer />, mountNode);
An Application
Using props
and state
, we can put together a small Todo application. This example uses state
to track the current list of items as well as the text that the user has entered. Although event handlers appear to be rendered inline, they will be collected and implemented using event delegation.
var TodoList = React.createClass({ render: function() { var createItem = function(itemText) { return <li>{itemText}</li>; }; return <ul>{this.props.items.map(createItem)}</ul>; } }); var TodoApp = React.createClass({ getInitialState: function() { return {items: [], text: ''}; }, onChange: function(e) { this.setState({text: e.target.value}); }, handleSubmit: function(e) { e.preventDefault(); var nextItems = this.state.items.concat([this.state.text]); var nextText = ''; this.setState({items: nextItems, text: nextText}); }, render: function() { return ( <div> <h3>TODO</h3> <TodoList items={this.state.items} /> <form onSubmit={this.handleSubmit}> <input onChange={this.onChange} value={this.state.text} /> <button>{'Add #' + (this.state.items.length + 1)}</button> </form> </div> ); } }); React.render(<TodoApp />, mountNode);
A Component Using External Plugins
React is flexible and provides hooks that allow you to interface with other libraries and frameworks. This example uses Showdown, an external Markdown library, to convert the textarea’s value in real-time.
var converter = new Showdown.converter(); var MarkdownEditor = React.createClass({ getInitialState: function() { return {value: 'Type some *markdown* here!'}; }, handleChange: function() { this.setState({value: this.refs.textarea.getDOMNode().value}); }, render: function() { return ( <div className="MarkdownEditor"> <h3>Input</h3> <textarea onChange={this.handleChange} ref="textarea" defaultValue={this.state.value} /> <h3>Output</h3> <div className="content" dangerouslySetInnerHTML={{ __html: converter.makeHtml(this.state.value) }} /> </div> ); } }); React.render(<MarkdownEditor />, mountNode);
More info
http://facebook.github.io/react/docs/getting-started.html
https://facebook.github.io/react/downloads.html
https://github.com/react-bootstrap/react-bootstrap