[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>
[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 framewo ...
- React/React Native 的ES5 ES6写法对照表
//es6与es5的区别很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component ...
- React/React Native 的ES5 ES6写法对照表-b
很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component),然而网上搜到的很多教 ...
- [React] react+redux+router+webpack+antd环境搭建一版
好久之前搭建的一个react执行环境,受历史影响是webpack3.10.0和webpack-dev-server2.7.1的环境,新项目准备用webpack4重新弄弄了,旧的记录就合并发布了(在没有 ...
- React: React组件的生命周期
一.简介 在前面的第二篇博文中对组件的生命周期虽然做了一个大略介绍,但总感觉说的过于简单,毕竟生命周期是React组件的核心部分.在我们熟练使用React挂载和合成组件来创建应用表现层的过程中,针对数 ...
- React: React的属性验证机制
一.简介 在开发中,属性变量类型的验证,几乎是任何语言都必须关注的问题,因为如果传入的数据类型不对,轻者程序运行仅仅是给出警告⚠️,严重的会直接导致程序中断,APP闪退或者web页面挂掉,这是很严重的 ...
- React/react相关小结
React React组件由React元素组成,React组件使用React.Component或React.PureComponent来生成:React元素使用JSX的语法来编写或使用React.c ...
- [React] React Fundamentals: Add-on ClassSet() for ClassName
To get the add-ons, use react-with-addons.js (and its minified counterpart) rather than the common r ...
- [React] React Fundamentals: Mixins
Mixins will allow you to apply behaviors to multiple React components. Components are the best way t ...
随机推荐
- Lsp修复
打开电脑,进入命令提示符窗口,快捷键win+r. 在窗口中输入“cmd”进入命令符窗口. 在窗口中输入:输入netsh winsock reset,然后按下回车键. 然后稍等片刻, ...
- hw-text1
Text 1 测试题 python是什么类型的语言? 解释型语言,是脚本语言 百娘(脚本语言是为了缩短传统的编写-编译-链接-运行(edit-compile-link-run)过程而创建的计算机编程语 ...
- python包管理器pip
步骤一:下载pip包 https://pypi.python.org/pypi/pip 步骤二:安装pip包 解压后,到pip包目录执行: python setup.py install 步骤三:添加 ...
- Python定义常量
用Python实现常量 定义 # coding=utf-8 # const.py class ConstAssignError(Exception): pass class _const(object ...
- 读书笔记-《Training Products of Experts by Minimizing Contrastive Divergence》
Training Products of Experts by Minimizing Contrastive Divergence(以下简称 PoE)是 DBN 和深度学习理论的 肇始之篇,最近在爬梳 ...
- 关于一个简单面试题(。net)
猫大叫一声,主人被惊醒,所有的小老鼠开始逃窜. 期初想到的是事件调用方法. 在猫叫的事件中调用一对方法就可以了. 但是,当事件很多的时候 难保大家写着写着就忘记了. 总不能有 10000个人的时候调用 ...
- iOS:导航栏的工具条和导航条
功能:用NAV视图控制器打开新的视图,默认工具条和导航条隐藏,双击显示之 // // main.m // Hello // // Created by lishujun on 14-8-28. // ...
- jQuery异步表单提交
有时在A页面点击按钮弹出一个form表单,在填完表单后提交成功后,需要关闭表单页并将表单中的某些值反应在A页面上,这时就需要异步提交表单.其实也挺简单,只是需要把表单数据序列化. $("#f ...
- Contest 20140914 Mushroom写情书 字符串雙hash 後綴數組
0111:Mushroom写情书 查看 提交 统计 提问 总时间限制: 10000ms 内存限制: 256000kB 描述 有一天,Mushroom准备向他的GF表白,为了增加表白成功率,Mush ...
- Codeforces 446-C DZY Loves Fibonacci Numbers 同余 线段树 斐波那契数列
C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...