• React.js学习笔记(一):组件协同与mixin


    组件协同:

    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta charset="UTF-8">
        <title>父子关系</title>
    </head>
    <body>
    <script src="react.js"></script>
    <script src="JSXTransformer.js"></script>
    <script type="text/jsx">
        var GenderSelect = React.createClass({
            render: function() {
                return <select onChange={this.props.handleSelect}>
                    <option value="0"></option>
                    <option value="1"></option>
                </select>
            }
        });
    
        var SignUpForm = React.createClass({
            getInitialState:function(){
                return {
                    name:"",
                    password:"",
                    gender:""
                }
            },
            handleChange:function(name,event){
                var newState = {};
                newState[name] = event.target.value;
                this.setState(newState);
            },
            handleSelect: function (event) {
                this.setState({gender: event.target.value})
            },
            render: function () {
                console.log(this.state);
                return <form>
                    <input type="text" placeholder="请输入用户名" onChange = {this.handleChange.bind(this,'name')}/>
                    <input type="password" placeholder="请输入密码" onChange = {this.handleChange.bind(this,'password')}/>
                    <GenderSelect handleSelect={this.handleSelect}></GenderSelect>
                </form>
            }
        });
        React.render(<SignUpForm></SignUpForm>,document.body);
    </script>
    </body>
    </html>
    View Code

    mixin:

    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta charset="UTF-8">
        <title>Mixin</title>
    </head>
    <body>
    <script src="react.js"></script>
    <script src="JSXTransformer.js"></script>
    <script type="text/jsx">
        var BindingMixIn = {
            handleChange: function (key) {
                var that = this;
                return function(event){
                    var newState = {};
                    newState[key] = event.target.value;
                    that.setState(newState);
                }
            }
        };
        var BindingExample = React.createClass({
            mixins:[BindingMixIn],
            getInitialState: function () {
                return {
                    text: '',
                    comment:''
                }
            },
    
            render: function () {
                return <div>
                    <input type="text" placeholder="请输入内容" onChange={this.handleChange('text')}/>
                    <textarea placeholder="请输入内容" onChange={this.handleChange('comment')}></textarea>
                    <p>{this.state.text}</p>
                    <p>{this.state.comment}</p>
    
                </div>
            }
        })
        React.render(<BindingExample></BindingExample>, document.body)
    </script>
    </body>
    </html>
    View Code
  • 相关阅读:
    Python爬虫之Scrapy框架的简介和基础应用
    python爬虫之selenium,谷歌无头浏览器
    12.块设备驱动程序(磁盘)
    11.USB输入子系统
    10.触摸屏驱动
    9.LCD驱动架构
    8.platform驱动分离
    7.输入子系统框架
    5.标签管理
    4.分支管理
  • 原文地址:https://www.cnblogs.com/shuiyi/p/4929099.html
Copyright © 2020-2023  润新知