ReactJS中的自定义组件

2019-11-23 16:01:26来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

ReactJS中的自定义组件

可控自定义组件:

<!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>
<!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>

本人刚建立一个前端学习交流群,群内有很多干货适合前期和中期需要解决的问题,欢迎给位进群一起探讨互相帮助907694362祝各位在前端的道路上一帆风顺!!

 


原文链接:https://www.cnblogs.com/xsd1/p/11917746.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:UML:类图关系总结

下一篇:【前端开发环境】前端使用GIT管理代码仓库需要掌握的几个必备技