React JS的基本用法[ES5,純前端寫法]

來源:互聯網
上載者:User

標籤:button   php   initial   系統   min   color   eve   tde   class   

參照:http://www.ruanyifeng.com/blog/2015/03/react.html

 

注意事項:1.必須放倒伺服器上,在檔案系統上無法運行

 

login.html

<!doctype html><head>    <meta charset="utf-8">    <script src="https://npmcdn.com/[email protected]/dist/react-with-addons.min.js"></script>    <script src="https://npmcdn.com/[email protected]/dist/react-dom.min.js"></script>    <script src="https://npmcdn.com/[email protected]/browser.min.js"></script>    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.2/css/bootstrap.min.css">    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">    <script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script>    <script src="http://cdn.bootcss.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>    <link rel="stylesheet" href="login.css" type="text/css" />    <title>        頁面    </title></head><body style="margin: auto"><div id="root"></div><script type="text/babel" src="login.js"></script></body></html>

  

 

login.css

.login{    background-color: red;}.header{    height: 30px;    background-color: gray;}.userRole{    height:80px;    background-color: lightcyan;}.userId{}.userPassword{}.submitButtonEnabled{    color:blue;}.submitButtonDisabled{    color: gray;}

  

login.js

var Login = React.createClass({    //props的類型和是否必須填寫    propTypes: {        title: React.PropTypes.string.isRequired,        role: React.PropTypes.string.isRequired,    },    getDefaultProps: function() {        console.log("getDefaultProps");        return {            title: ‘預設title‘,            role: ‘預設role‘,        };    },    //初始化state    getInitialState: function() {        console.log("getInitialState");        return {            userId: ‘‘,            userPassword: ‘‘,            submitEnabled: false,        };    },    componentWillMount: function () {        console.log("componentWillMount");        this.timer = setInterval(function () {        }.bind(this), 100);    },    componentDidMount: function () {        console.log("componentDidMount");        this.timer = setInterval(function () {        }.bind(this), 100);    },    componentWillUnmount:function () {        console.log("componentWillUnmount");        this.timer = setInterval(function () {        }.bind(this), 100);    },    //event    handleClickSubmit:function () {        if(this.state.userId.length < 1 || this.state.userPassword.length < 1){            return;        }        alert("submit");        $.get("http://publicschool.sinaapp.com/test/test.php?name=jack", function(result) {            console.log(result);        }.bind(this));//綁定this後,`回呼函數`才能使用`this`    },    handleChangeId:function () {        this.setState({            userId:this.refs._ref_userId.value,            submitEnabled:this.refs._ref_userId.value.length > 0 && this.refs._ref_userPassword.value.length > 0,        });    },    handleChangePassword:function () {        this.setState({            userPassword:this.refs._ref_userPassword.value,            submitEnabled:this.refs._ref_userId.value.length > 0 && this.refs._ref_userPassword.value.length > 0,        });    },    //渲染方法    render: function () {        return(            <div className="login">                <div className="header">                    {this.props.title}                </div>                <div className="userRole">                    {this.props.role}                </div>                <input className="userId" type="text" ref="_ref_userId" placeholder="使用者名稱" onChange={this.handleChangeId} />                <input className="userPassword" type="password" ref="_ref_userPassword" placeholder="密碼" onChange={this.handleChangePassword} />                <button type="button" onClick={this.handleClickSubmit}                        className={this.state.submitEnabled?"submitButtonEnabled":"submitButtonDisabled"}>登入</button>            </div>        );    },});ReactDOM.render(    <Login title="我是標題" role="學生的家長"/>,    document.getElementById(‘root‘));

  

React JS的基本用法[ES5,純前端寫法]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.