[React] React Fundamentals: with-addons - ReactLink

It can be tedious to type out all the boilerplate needed to get the DOM and states in React to synchronize. Luckily, React provides a version of the toolkit with a selection of available addons. This lesson is going to dig into ReactLink, and how this addon can give you two-way binding.

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>React Lesson 15: dynamically create componenets</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.12.2/react.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.12.2/JSXTransformer.js"></script>
    <style>
        body {
            margin: 25px;
        }
    </style>
</head>
<body>
<div id="panel"></div>
<script type="text/jsx">
    /** @jsx React.DOM */

    var App = React.createClass({
        getInitialState:function(){
            return {
                name: ‘‘,
                email: ‘‘
            }
        },
        update: function () {
           this.setState({
               name: this.refs.name.getDOMNode().value,
               email: this.refs.email.getDOMNode().value
           })
        },
        render:function(){
            return (
                <form>
                    <div>
                        <input type="text" ref="name" onChange={this.update} placeholder="Name"/>
                        <label>*{this.state.name}*</label>
                    </div>
                    <div>
                        <input type="text" ref="email" onChange={this.update} placeholder="Email"/>
                        <label>*{this.state.email}*</label>
                    </div>
                </form>
            );
        }

    });

    React.render(<App />, document.getElementById(‘panel‘));
</script>
</body>
</html>

Use addon: ReactLink

1. include the script:

script src="https://fb.me/react-with-addons-0.13.3.js"></script>

2. Add mixin:

mixins: [React.addons.LinkedStateMixin],

3. Use valueLink={this.linkState(‘name‘)} instead of ‘ref‘ and ‘onChange‘:

<input valueLink={this.linkState(‘name‘)} type="text" placeholder="Name" />

Code:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>React Lesson 15: dynamically create componenets</title>
    <script src="https://fb.me/react-with-addons-0.13.3.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/JSXTransformer.js"></script>
    <style>
        body {
            margin: 25px;
        }
    </style>
</head>
<body>
<div id="panel"></div>
<script type="text/jsx">
    /** @jsx React.DOM */

    var App = React.createClass({
        mixins: [React.addons.LinkedStateMixin],
        getInitialState:function(){
            return {
                name: ‘‘,
                email: ‘‘
            }
        },
        render:function(){
            return (
                <form>
                    <div>
                        <input valueLink={this.linkState(‘name‘)} type="text" placeholder="Name" />
                        <label>*{this.state.name}*</label>
                    </div>
                    <div>
                        <input valueLink={this.linkState(‘email‘)} type="text" placeholder="Email" />
                        <label>*{this.state.email}*</label>
                    </div>
                </form>
            );
        }

    });

    React.render(<App />, document.getElementById(‘panel‘));
</script>
</body>
</html>
时间: 2024-10-12 13:38:52

[React] React Fundamentals: with-addons - ReactLink的相关文章

[React] React Fundamentals: Integrating Components with D3 and AngularJS

Since React is only interested in the V (view) of MVC, it plays well with other toolkits and frameworks. This includes AngularJS and D3. A app with React and D3.js: /** @jsx React.DOM */ var App = React.createClass({ getInitialState: function () { re

React/React Native 的ES5 ES6写法对照表

转载: http://bbs.reactnative.cn/topic/15/react-react-native-%E7%9A%84es5-es6%E5%86%99%E6%B3%95%E5%AF%B9%E7%85%A7%E8%A1%A8 英文版: https://babeljs.io/blog/2015/06/07/react-on-es6-plus 很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends

[React] React Fundamentals: Precompile JSX

The JSX Transformer library is not recommended for production use. Instead, you'll probably want to precompile your JSX into JavaScript. Install: npm install react-tools -g Run: jsx dev build --no-cache-dir --watch //watch dev dir and compile js to b

[React] React Router: Nested Routes

Since react-router routes are components, creating nested routes is as simple as making one route a child of another in JSX. Make the nested component: class App extends React.Component { render(){ return( <Router history={hashHistory}> <Route pa

[React] React Router: IndexRoute

IndexRoute allows us to define a default child component to be rendered at a specific route when no other sub-route is available. When Home page display, we also make About component as default Route to dsiplay, only when use click Contact link, then

[React] React Router: Router, Route, and Link

In this lesson we'll take our first look at the most common components available to us in react-router; Router, Route, and Link. import React from 'react'; import {hashHistory, Route, Router, Link} from 'react-router'; const Home = () => <div><

[React] React Router: Named Components

In this lesson we'll learn how to render multiple component children from a single route. Define a named component by "components": <Route path="/other" components={ {header: Other, body: OtherBody}}></Route> 'header' and '

[React] React Router: Querystring Parameters

Define query param in Link, accept path and query : const Links = () => <nav > <Link to={{path: '/', query: {message: 'Yo'}}}>Home</Link> </nav>; Use Query param by props.location.query: const Container = (props) => <div&g

[React] React Router: Redirect

The Redirect component in react-router does exactly what it sounds like. It allows us to redirect from from one route to another. import React from 'react'; import {hashHistory, Route, Redirect, Router, Link} from 'react-router'; const Home = () => <