zoukankan      html  css  js  c++  java
  • react.js 各种小测试笔记

    首先看一个 基础html  至于其中的 js 问价大家去官网下载就好了。

    <html>
    <head>
    <script src="../build/react.js"></script>
    <script src="../build/react-dom.js"></script>
    <script src="../build/browser.min.js"></script>
    </head>
    <body>
    <div id="test"></div>
    <script type="text/babel">
    
    </script>
    </body>
    </html>

    1在input框中输入值点击按钮获取其中的值在console.log中打印。

    var TestRef = React.createClass({
                handleClick : function(){
                    console.log(this.refs.Inputref.value);
                },
                render:function(){
                    return (<div>
                    <input type="text" ref="Inputref"/>
                    <input type="button" value="TEXT" onClick={this.handleClick}/>
                </div>);
    
                }
            });
    
            ReactDOM.render(
                <TestRef/>,
                 document.getElementById('test')
            );

    2

    //2 组建通讯 属性的用法
    /**
    * 当输入框的值改变的时候上面hello后面的值也发生改变
    * 当Test1 组建创建 执行getInitialState(这个方法只运行一次 在创建组建的时候)方法 设置状态 name 的默认值为空
    * 然后执行render 方法 渲染 此时调用Test组建 由于组建Test中name 属性的值是空 所以页面第一次会显示 hello word
    * 当用户在输入框中输入数据的时候 出发 onChange 执行handleChange 方法 这个方法中
    * setState 更新name 的状态为输入框的值。此时在组建Test 中的属性name 的值发生改变 所以会随之变化
    */

    var Test =React.createClass({
            render:function(){
                return <div>hello,{this.props.name ? this.props.name :'word!'}!</div>;
            }
        });
        var Test1 = React.createClass({
            getInitialState:function(){
                return {name:''};
            },
            handleChange:function(event){
                this.setState({name:event.target.value})
            },
            render:function(){
                return(
                        <div>
                            <Test name={this.state.name}/>
                            <input type="text" onChange={this.handleChange}/>
                        </div>
                    );
            }
        });
        
        ReactDOM.render(
            <Test1/>,
             document.getElementById('test')
        );

    // 组建通讯 状态 属性的用法
    /**
    * 状态,属性的用法
    * 场景:点击提交的时候 获取textarea的值和select 选中的值。用console.log 打印出来
    * 最后render 渲染的时候是Test1组建 所以先看Test1组建 创建的时候 定义了2个状态 names数组 selectvalue
    * 渲染的时候 也加载了<Test>组建 给组建设置了一个属性selectName 值等于Test1中的状态 selectvalue
    * 组建Test1中当下拉框的值改变的时候触发handleOnchange方法 设置状态 selectvalue的值为下拉框中选中的值。
    * 在Test 组建中 创建的时候 设置状态 inputValue为空 渲染的时候 当输入框的值改变的时候出发handleONE方法
    * 设置状态inputValue 的值为输入框的值。 当点击Submit的时候出发 handleTWO 方法 获取状态inputValue的值和属性selectName的值
    *

        var Test =React.createClass({
            getInitialState:function(){
                return {inputValue:''};
            },
            handleONE:function(event){
                this.setState({inputValue:event.target.value});
            },
            handleTWO:function(){
                console.log("textarea value is:"+this.state.inputValue+"----selectValue:"+this.props.selectName);
            },
            render:function(){
                return (
                    <div>
                    <textarea placeholder="please input string" onChange={this.handleONE}>
                    </textarea>
                    <input type="button" value="Submit" onClick={this.handleTWO}/>
                    </div>
                    );
            }
        });
    
        var Test1 = React.createClass({
            getInitialState:function(){
                return{
                    names:['xiaoming','xiaowang','xiaohong'],
                    selectvalue:'',
                } 
            },
            handleOnchange:function(event){
                this.setState({selectvalue:event.target.value});
            },
            render:function(){
                var optionArr = [];
                for (var option in this.state.names) {
                    optionArr.push(<option key={option} value={this.state.names[option]}>{this.state.names[option]}</option>);
                };
                return(
                        <div>
                        <select onChange={this.handleOnchange}> 
                            {optionArr}
                        </select>
                        <Test selectName={this.state.selectvalue}/>
                        </div>
                    );
            }
    
        });
        ReactDOM.render(
            <Test1/>,document.getElementById('test')
            );

    4 组建的生命周期  初始化阶段

    // React   有三个阶段 初始化阶段 运行中阶段 销毁阶段
    // React  初始化阶段
    var HelloWorld = React.createClass({
            //设置默认属性
                getDefaultProps: function () {
                    console.log("getDefaultProps, 1")
                },
            //设置默认状态    
                getInitialState: function () {
                    console.log("getInitialState, 2");
                    return null;
                },
            //在渲染之前调用
                componentWillMount: function () {
                    console.log("componentWillMount, 3")
                },
            //渲染
                render: function () {
                    console.log("render, 4")
                    return <p ref="childp">Hello, {(function (obj) {
                        if (obj.props.name)
                            return obj.props.name
                        else
                            return "World"
                    })(this)}</p>
                },
            //组建渲染完成之后调用
                componentDidMount: function () {
                    console.log("componentDidMount, 5")
                },
            });
            ReactDOM.render
            (<div>
                <HelloWorld></HelloWorld>
                </div>,
                 document.getElementById('test')
                );

    看结果

    5 组建的生命周期 运行中阶段

    // React   有三个阶段 初始化阶段 运行中阶段 销毁阶段
    // React  运行中阶段触发顺序
    var HelloWorld = React.createClass({
                componentWillReceiveProps: function () {
                    console.log("componentWillReceiveProps 1");
                },
                shouldComponentUpdate: function () {
                    console.log("shouldComponentUpdate 2");
                    return true;
                },
                componentWillUpdate: function () {
                    console.log("componentWillUpdate 3")
                },
                render: function () { 
                    console.log("render 4");
                    return <p>Hello, {this.props.name ? this.props.name : "World"}</p>;
                },
                componentDidUpdate: function() {
                    console.log("componentDidUpdate 5");
                },
            });
            var HelloUniverse = React.createClass({
                getInitialState: function () {
                    return {name: ''};
                },
                handleChange: function (event) {
                    this.setState({name: event.target.value});
                },
                render: function () {
                    return <div>
                    <HelloWorld name={this.state.name}></HelloWorld>
                    <br/>
                    <input type="text" onChange={this.handleChange} />
                    </div>
                },
            });
            ReactDOM.render
            (<div>
                <HelloUniverse></HelloUniverse>
                </div>,
                 document.getElementById('test')
                );

    默认情况下   当输入数据时

    6  mixin 的用法 :复用  类似于公共方法  提高代码的复用性

    //Mixin  用法
        var MixinFunction ={
            handleChange:function(key){
                var that =this
                // 这里的this  代表的是HelloUniverse  这个组建。
                //如果return 中用this 的话 this 代表的就是 这个函数的本身  所以用that变量去代替
                return function(event){
                    var Common={};
                    Common[key] = event.target.value;
                    that.setState(Common);
                }
            }
        };
    
        var HelloUniverse = React.createClass({
            mixins:[MixinFunction],
            getInitialState:function(){
                return {name:'',name2:''};
            },
            
            render:function(){
                return(
                    <div>
                    <input type="text" onChange={this.handleChange('name')}/><hr/>
                    <input type="text" onChange={this.handleChange('name2')}/>
                    <p>input_value_name:{this.state.name}</p>
                    <p>input_value_name2:{this.state.name2}</p>
                    </div>
                );            
            }
        });
    
    
        ReactDOM.render(
            <div>
                <HelloUniverse></HelloUniverse>
            </div>,
            document.getElementById('test')
        );

    看结果:

     初始: 结果:实现了双向绑定 

  • 相关阅读:
    Algorithm --> KMP算法
    Algorithm --> 快速排序
    Algorithm --> 阶乘和因子
    Algorithm --> Dijkstra和Floyd最短路径算法
    Algorithm --> 二分图最大匹配
    Algorithm --> 邮票连续组合问题
    Algorithm --> 字母重排
    Algorithm --> 6174问题
    Algorithm --> 字符串中最长不重合子串长度
    Algorithm --> DFS和BFS
  • 原文地址:https://www.cnblogs.com/yhl664123701/p/5870291.html
Copyright © 2011-2022 走看看