Mixins will allow you to apply behaviors to multiple React components.

Components are the best way to reuse code in React, but sometimes very different components may share some common functionality. These are sometimes called cross-cutting concerns. React provides mixins to solve this problem.

https://facebook.github.io/react/docs/reusable-components.html

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>React Lesson 12: Mixins</title>
<script src="http://fb.me/react-0.8.0.js"></script>
<script src="http://fb.me/JSXTransformer-0.8.0.js"></script>
<style>
body {
margin: 25px;
}
</style>
</head>
<body>
<div id="panel"></div>
<script type="text/jsx">
/** @jsx React.DOM */ var ReactMixin = {
componentWillMount: function(){
console.log("will mount")
},
getInitialState:function(){
return {count:0}
},
incrementCount:function(){
this.setState({count:this.state.count+1})
}
}; var APP = React.createClass({ render: function(){
return (
<div>
<ButtonComponent val="This is a button"></ButtonComponent>
<InputComponent val="this is a input"></InputComponent>
</div>
);
}
}); var ButtonComponent = React.createClass({
mixins: [ReactMixin],
render: function(){
return (
<button onClick={this.incrementCount}>{this.props.val} -- {this.state.count}</button>
);
}
}); var InputComponent = React.createClass({
mixins: [ReactMixin],
//Can double the function in the mixin, React will combine both
componentWillMount: function(){
this.inc = setInterval(this.incrementCount, 500);
},
unmount: function() {
this.componentWillUnmount();
},
render: function() {
return (
<div>
<input type="text" value={this.props.val +' -- '+ this.state.count}/>
<button onClick={this.unmount}>unmount</button>
</div>
);
},
componentWillUnmount: function() {
clearInterval(this.inc);
}
}); React.renderComponent(
<APP />,
document.getElementById('panel')) </script>
</body>
</html>

[React] React Fundamentals: Mixins的更多相关文章

  1. [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 ...

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

    //es6与es5的区别很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component ...

  3. React/React Native 的ES5 ES6写法对照表-b

    很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component),然而网上搜到的很多教 ...

  4. [React] react+redux+router+webpack+antd环境搭建一版

    好久之前搭建的一个react执行环境,受历史影响是webpack3.10.0和webpack-dev-server2.7.1的环境,新项目准备用webpack4重新弄弄了,旧的记录就合并发布了(在没有 ...

  5. React: React组件的生命周期

    一.简介 在前面的第二篇博文中对组件的生命周期虽然做了一个大略介绍,但总感觉说的过于简单,毕竟生命周期是React组件的核心部分.在我们熟练使用React挂载和合成组件来创建应用表现层的过程中,针对数 ...

  6. React: React的属性验证机制

    一.简介 在开发中,属性变量类型的验证,几乎是任何语言都必须关注的问题,因为如果传入的数据类型不对,轻者程序运行仅仅是给出警告⚠️,严重的会直接导致程序中断,APP闪退或者web页面挂掉,这是很严重的 ...

  7. React/react相关小结

    React React组件由React元素组成,React组件使用React.Component或React.PureComponent来生成:React元素使用JSX的语法来编写或使用React.c ...

  8. [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 synch ...

  9. [React] React Fundamentals: Component Lifecycle - Updating

    The React component lifecycle will allow you to update your components at runtime. This lesson will ...

随机推荐

  1. Motion on Ubuntu

    Motion is a program that monitors the video signal from one or more cameras and is able to detect if ...

  2. [jobdu]用两个栈实现队列

    思路比较简单.就是当要pop的时候,如果s2为空,才把s1的转过来.总之就是区分一下此时s2为空和非空的情况. http://ac.jobdu.com/problem.php?pid=1512 #in ...

  3. thinkphp 模板替换

    具体详见tp手册. 如果需要修改模板替换映射路径. 则需: 'TMPL_PARSE_STRING'=>array( '__PUBLIC__'=>__ROOT__.'/'.APP_NAME. ...

  4. 156. Binary Tree Upside Down

    题目: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node ...

  5. wzplayer for ios 针对(mms)优化版本V1.0

    wzplayer for ios针对mms优化版本发布. 1.支持mms,http,rtmp,rtsp等协议 2.支持全格式 下载地址:http://www.coolradio.cn/WzPlayer ...

  6. 转 Xcode磁盘空间大清理

    http://www.iwangke.me/2013/09/09/clean-xcode-to-free-up-disk-space/#jtss-tsina 我的设备是Macbook Air 13’ ...

  7. jquery插件cloud-zoom(放大镜)

    效果预览:http://www.helloweba.com/demo/cloud-zoom/ 源代码下载:http://pan.baidu.com/s/1eQnadXo Cloud Zoom是一个图像 ...

  8. 用excel打造报表查询系统

    网络数据库以及ERP在中小型企业中日益风行,虽然ERP功能强大,但有的ERP报表系统中规范的报表较少,主要提供二次开发接口或通过如CRYSTALREPORT等其他报表工具进行管理,其实我们可以使用Ex ...

  9. Appium 小白从零安装 ,Appium连接真机测试。

    以下是我个人在初次安装使用Appium时的过程,过程中遇到了一些问题,在这里也一一给出解决办法. Appium安装过程 先安装了 Node.js.在node的官网上下载的exe安装文件. 在node的 ...

  10. ActiveMQ中的安全机制 [转]

    本文简单介绍ActiveMQ通过JAAS实现的安全机制.JAAS(Java Authentication and Authorization Service)也就是java认证/授权服务.这是两种不同 ...