ReactJS中的自定义组件
可控自定义组件:
<!DOCTYPE html> <html> <head> <meta charset=http://www.mamicode.com/"UTF-8"> <title></title> <script src=http://www.mamicode.com/"js/react.js"></script> <script src=http://www.mamicode.com/"js/react-dom.js"></script> <script src=http://www.mamicode.com/"js/browser.min.js"></script> </head> <body> <script type=http://www.mamicode.com/"text/babel"> var Radio=http://www.mamicode.com/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=http://www.mamicode.com/[]; var value=http://www.mamicode.com/this.props.value||this.state.value; React.Children.forEach(this.props.children,function(child,i){ var label=<label key={i}> <input type=http://www.mamicode.com/"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=http://www.mamicode.com/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=http://www.mamicode.com/"my_radio" onChange={this.handleChange}> <option value=http://www.mamicode.com/"A">First option</option> <option value=http://www.mamicode.com/"B">Second option</option> <option value=http://www.mamicode.com/"C">Third option</option> </Radio> <button type=http://www.mamicode.com/"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="http://www.mamicode.com/js/react.js"></script> <script src="http://www.mamicode.com/js/react-dom.js"></script> <script src="http://www.mamicode.com/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" 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>温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/42894.html
- 上一篇:Tomcat Response encode
- 下一篇:沃土前端系列