the component of reactjs
2016-09-08 16:49
369 查看
react的组件之间的传值可以有三种情况,分别是
(1)父组件——子组件 主要利用props属性
(2)子组件——父组件 借助于函数的传递,利用了类似的回调函数
(3)兄弟组件之间的值传递 第三方思想PubSub (还有就是子1->父,父->子2)
下面的程序演示了如上三种情况
react的组件化:
这张图构成了如上页面的全部,每种颜色的方框就是一个组件,也可以看出它们之间的关系,组件封装这种开发模式非常好用,极大提高了效率。
red means div1
green means Commsg
blue means Parcom
purple means Childcom
(1)父组件——子组件 主要利用props属性
(2)子组件——父组件 借助于函数的传递,利用了类似的回调函数
(3)兄弟组件之间的值传递 第三方思想PubSub (还有就是子1->父,父->子2)
下面的程序演示了如上三种情况
<body> <div id="div1"></div> <script src='react-0.14.7/build/react.js'></script> <script src='react-0.14.7/build/react-dom.js'></script> <script src='browser.js'></script> <script src='PubSub.js'></script> <script type='text/babel'> var Commsg = React.createClass({ getInitialState:function(){ return { <span style="white-space:pre"> </span>content:0 }; }, componentWillMount:function(){ PubSub.subscribe('select',function(msg,data){ this.setState({ content:data.value }); }.bind(this)); }, componentWillUnmount:function(){ PubSub.unsubscribe('select'); }, render:function(){ return ( <div >你掌握的语言种类个数:{this.state.content}</div> ) } }); var Childcom = React.createClass({ render:function(){ var array=[]; this.props.arr.forEach(function(value,index,arr){ array.push(<label key={index}><span>{value}</span><input type='checkbox' onChange={this.props.on}/></label>); }.bind(this)); return (<div>{array}</div>) } }); var Parcom = React.createClass({ getInitialState:function(){ return { count:0 }; }, change:function(e){ this.setState({ count:e.target.checked?this.state.count+ 1:this.state.count- 1 }); }, render:function(){ var arr=['c++','java','node']; return ( <div> <h1 onClick={this.doChange}>the countis:{this.state.count}</h1> <Childcom arr={arr} on={this.change}/> </div> ); }, doChange:function(){ PubSub.publish('select',{ value:this.state.count }); }, }); ReactDOM.render( <div> <Commsg /> <Parcom /> </div>, document.getElementById('div1') ); </script> </body>
react的组件化:
这张图构成了如上页面的全部,每种颜色的方框就是一个组件,也可以看出它们之间的关系,组件封装这种开发模式非常好用,极大提高了效率。
red means div1
green means Commsg
blue means Parcom
purple means Childcom
相关文章推荐
- <React: Up and Running>阅读笔记 — The life of a component & Excel: a fancy table component
- 使用js隐藏GridView的表头(hide the table header of GridView by JS)
- [Javascript] A function works like 'print_r()' in PHP to print out the details of an object for JS debugging
- Reactjs学习 -- factory、component和element
- The React.js Way系列之 通过使用 Immutable.js 的 Flux 建筑结构
- js - SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data jquery-1.9.1.min.js:3:4315
- The Power of Prototype.js
- exactly the kind of division of tasks that Gulp.js is built on
- The basics of using ui-router with AngularJS
- http://browniefed.com/blog/2015/09/10/the-shapes-of-react-native/
- The basics of using ui-router with AngularJS
- Business Component Factory : A Comprehensive Overview of Component-Based Development for the Enterpr
- Siebel Exception: The value entered in field Business Component of buscomp Repository WF Step does not match any value in the bo
- Siebel Exception: The value entered in field Business Component of buscomp Repository WF Step does not match any value in the bo
- js detect the type of device
- 'Agent XPs' component is turned off as part of the security configuration for this server
- the default width and heigth of all the component is 0!!!
- 使用js隐藏GridView的表头(hide the table header of GridView by JS)
- The beauty of node.js
- java.lang.RuntimeException: Date pattern must be set for column update_time in the schema of component