zoukankan      html  css  js  c++  java
  • react 三目运算符

    var Divider=React.createClass({
                getInitialState:function(){
                    return {isComplete:false}
                },
                render:function(){
                    return (
                            <div className={this.state.isComplete ? 'is_complete' : ''}>
                                <h2>{this.props.children}</h2><hr />
                            </div>
                        
                        )
                }
            });
            React.render(
                <Divider />questions</Divider>,
                document.body
            )

    前面的引入JS文件就不续写了,直接上代码,可以试试看,一定要给一个初始值getInitialState,要不然会报错!

    使用变量的三目运算符:

    var Divider=React.createClass({
                getInitialState:function(){
                    return {isComplete:true}
                },
                getIsComplete:function(){
                    return this.state.isComplete ? 'is_complete' :'';
                },
                render:function(){
                    var isComplete=this.getIsComplete();
                    return (
                        <div className={isComplete}>
                            <h2>{this.props.children}</h2>
                        </div>
                    )
                }
            });
            React.render(
                <Divider>questions</Divider>,
                document.body
            )

     使用函数的三目运算符:

    var Divider=React.createClass({
                getInitialState:function(){
                    return {isComplete:true}
                },
                getIsComplete:function(){
                    return this.state.isComplete ? 'is_complete' :'';
                },
                render:function(){
    
                    return (
                        <div className={this.getIsComplete()}>
                            <h2>{this.props.children}</h2>
                        </div>
                    )
                }
            });
            React.render(
                <Divider>questions</Divider>,
                document.body
            )
    如果问题,欢迎大家及时指点,一同交流,共同提高
  • 相关阅读:
    Svn如何使用,有什么作用?
    Unity脚本基础Day02
    unity设计模式-----责任链模式
    LitJson ---json的创建和解析
    Mesh编程——三角形,多边形,正方体,园形,圆环
    unity基础逻辑题
    unity——UI拖拽实现拼图
    unity:倒计时
    UGUI Toggle的监听事件绑定
    UnityGUI系统之InputField
  • 原文地址:https://www.cnblogs.com/wujidns/p/5623888.html
Copyright © 2011-2022 走看看