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
            )
    如果问题,欢迎大家及时指点,一同交流,共同提高
  • 相关阅读:
    HDU-1272-小希的迷宫(并查集)
    HDU-1084-What Is Your Grade?
    一个好的函数(gcd)求最小公约数
    HDU-1228-A + B
    HDU-1029-Ignatius and the Princess IV
    自控力》读后感·一
    HDU-2058-The sum problem(数学题技巧型)
    HDU-1430-素数回文
    sftp
    802. 区间和
  • 原文地址:https://www.cnblogs.com/wujidns/p/5623888.html
Copyright © 2011-2022 走看看