ReactJS中的自定义组件实例代码
前端  /  管理员 发布于 7年前   198
React 是一个用于构建用户界面的 JAVASCRIPT 库。
React 主要用于构建UI,很多人认为 React 是 MVC 中的 V(视图)。
React 起源于 Facebook 的内部项目,用来架设 Instagram 的网站,并于 2013 年 5 月开源。
React 拥有较高的性能,代码逻辑非常简单,越来越多的人已开始关注和使用它。
React 特点
1.声明式设计 −React采用声明范式,可以轻松描述应用。
2.高效 −React通过对DOM的模拟,最大限度地减少与DOM的交互。
3.灵活 −React可以与已知的库或框架很好地配合。
4.JSX − JSX 是 JavaScript 语法的扩展。React 开发不一定使用 JSX ,但我们建议使用它。
5.组件 − 通过 React 构建组件,使得代码更加容易得到复用,能够很好的应用在大项目的开发中。
6.单向响应的数据流 − React 实现了单向响应的数据流,从而减少了重复代码,这也是它为什么比传统数据绑定更简单。
可控自定义组件:
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <script src="https:/article/js/react.js"></script> <script src="https:/article/js/react-dom.js"></script> <script src="https:/article/js/browser.min.js"></script> </head> <body> <script type="text/babel"> var Radio=React.createClass({ getInitialState:function(){ return {value:this.props.defaultValue }; }, handleChange:function(event){ if(this.props.onChange){this.props.onChange(event); } this.setState({value:event.target.value }); }, render:function(){ var children=[]; var value=this.props.value||this.state.value; React.Children.forEach(this.props.children,function(child,i){var label=<label key={i}> <input type="radio" name={this.props.name} value={child.props.value} checked={child.props.value==value} onChange={this.handleChange}/> {child.props.children} <br/> </label>; children.push(label); }.bind(this)); return <span>{children}</span>; } }); var MyForm=React.createClass({ getInitialState:function(){ return ({my_radio:"B"}); }, handleChange:function(event){ this.setState({my_radio:event.target.value }); }, submitHandler:function(event){ event.preventDefault(); alert(this.state.my_radio); }, render:function(){ return (<form onSubmit={this.submitHandler}><Radio value={this.state.my_radio} name="my_radio" onChange={this.handleChange}> <option value="A">First option</option> <option value="B">Second option</option> <option value="C">Third option</option></Radio><button type="submit">Speak</button></form> ) } }); ReactDOM.render(<MyForm></MyForm>,document.body); </script> </body></html>
不可控的自定义组件:
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <script src="https:/article/js/react.js"></script> <script src="https:/article/js/react-dom.js"></script> <script src="https:/article/js/browser.min.js"></script> </head> <body> <script type="text/babel"> var Radio=React.createClass({ getInitialState:function(){ return {value:this.props.defaultValue }; }, handleChange:function(event){ if(this.props.onChange){this.props.onChange(event); } this.setState({value:event.target.value }); }, render:function(){ var children=[]; var value=this.props.value||this.state.value; React.Children.forEach(this.props.children,function(child,i){var label=<label> <input type="radio" name={this.props.name} value={child.props.value} checked={child.props.value==value} onChange={this.handleChange}/> {child.props.children} <br/> </label>; children['label'+i]=label; }.bind(this)); return <span>{children}</span>; } }); var MyForm=React.createClass({ handleSubmit:function(event){ event.preventDefault(); alert(this.refs.radio.state.value); }, render:function(){ return (<form onSubmit={this.handleSubmit}><Radio ref="radio" name="my_radio" defaultValue="B"> <p value="A">First</p> <option value="B">Second option</option> <option value="C">Third option</option></Radio><button type="submit">Speak</button></form> ) } }); ReactDOM.render(<MyForm></MyForm>,document.body); </script> </body></html><!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <script src="https:/article/js/react.js"></script> <script src="https:/article/js/react-dom.js"></script> <script src="https:/article/js/browser.min.js"></script> </head> <body> <script type="text/babel"> var Radio=React.createClass({ getInitialState:function(){ return {value:this.props.defaultValue }; }, handleChange:function(event){ if(this.props.onChange){this.props.onChange(event); } this.setState({value:event.target.value }); }, render:function(){ var children=[]; var value=this.props.value||this.state.value; React.Children.forEach(this.props.children,function(child,i){var label=<label> <input type="radio" name={this.props.name} value={child.props.value} checked={child.props.value==value} onChange={this.handleChange}/> {child.props.children} <br/> </label>; children['label'+i]=label; }.bind(this)); return <span>{children}</span>; } }); var MyForm=React.createClass({ handleSubmit:function(event){ event.preventDefault(); alert(this.refs.radio.state.value); }, render:function(){ return (<form onSubmit={this.handleSubmit}><Radio ref="radio" name="my_radio" defaultValue="B"> <p value="A">First</p> <option value="B">Second option</option> <option value="C">Third option</option></Radio><button type="submit">Speak</button></form> ) } }); ReactDOM.render(<MyForm></MyForm>,document.body); </script> </body></html>
总结
以上所述是小编给大家介绍的ReactJS中的自定义组件实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号