ReactJS中的自定义组件
来源:互联网 发布:音频制作软件手机软件 编辑:程序博客网 时间:2024/06/05 12:50
可控自定义组件
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <script src="js/react.js"></script> <script src="js/react-dom.js"></script> <script src="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="js/react.js"></script> <script src="js/react-dom.js"></script> <script src="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>
阅读全文
0 0
- ReactJS中的自定义组件
- ReactJs组件的生命周期及其在浏览器中的工作原理
- ReactJs组件的生命周期及其在浏览器中的工作原理
- ReactJs之组件生命周期
- ReactJs 组件间通信
- ReactJS可复用组件
- ReactJS表单组件
- ReactJS可复用组件
- ReactJS组件生命周期详述
- ReactJS组件生命周期详述
- reactjs,组件的生命周期
- ReactJS组件生命周期
- ReactJS组件的生命周期
- ReactJS读书笔记二:组件生命周期
- ReactJs 组件间数据传递
- ReactJS组件的生命周期详解
- ReactJS读书笔记二:组件生命周期
- ReactJs中的状态机State
- 排序+乱搞——数字查找
- 关于jquery对象与dom对象转换
- gogland出现could not launch process: exec: "lldb-server": executable file not found in $PATH错误
- JSTL标签库的基本教程之核心标签库
- SSL2505 2015年NOIP提高组试题 信息传递(并查集,最小环)
- ReactJS中的自定义组件
- ssm1
- 【git 使用详解(3)】-- 初体验
- CART之回归树python代码实现
- myeclipse 打开某些JSP报 Failed to create the part's controls 错误
- 10.30~10.31NOIP模拟赛总结
- Java程序性能优化 读书笔记(八)优化组件:缓冲和缓存
- Tomcat配置虚拟路径容易出现的问题
- 关于DbUtils组件