create-react-app

做任何東西以前須要搭建環境css

首先:安裝create-react-appnode

npm install -g create-react-app

使用命令建立myapp目錄react

create-react-app myapp

下一步:進入目錄命令npm

cd myapp

運行bootstrap

npm start

它會自動跳轉到瀏覽器
跳轉後再安裝命令segmentfault

npm install sass-loader node-sass --save-dev

上面就是環境搭建
環境搭建好後還須要下載bootstarp瀏覽器

能夠進去官網直接去下載bootstarpsass

也能夠參考博客參考博客app

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import LiuYanapp from './LiuYanapp';

import './bootstrap/css/bootstrap.min.css';
ReactDOM.render(<LiuYanapp/>,document.getElementById("app"));

LiuYanapp.js

import React from 'react';

import LiuYanList from './LiuYanList';
import LiuYanForm from './LiuYanForm';

class LiuYanapp extends React.Component{
    constructor(props){
        super(props);
        this.ids=1;
        this.state={
                todos:[]
        };
        
        this.addItem=this.addItem.bind(this);
        this.deleteItem=this.deleteItem.bind(this);
    }
    deleteItem(id){
        let newtodos=this.state.todos.filter((item)=>{
            return !(item.id==id)
        });
          this.setState({
            todos:newtodos
        });

    }

    addItem(value){
       this.state.todos.unshift(
            {
                id:this.ids++,
                text:value,
                time:(new Date()).toLocaleString(),
                done:0
            }
       )
        this.setState({
            todos:this.state.todos
        });   
    }

    render(){
        return (
            <div className="container">
                <br/>
                <br/>
                <br/>
                <br/>
                <div className="panel panel-default">
                    <div className="panel-headingbg-danger">
                         
                            <hr/>
                              
                    </div>
                    <div className="panel-body">           
                    
                             <h1 className="text-center ">都來留言啊!!!</h1>
                             <LiuYanList deleteItem={this.deleteItem} data={this.state.todos}/>
                             <LiuYanForm addItem={this.addItem}/> 
                    </div>
                </div> 
            </div>         
        );
    }
}

export default LiuYanapp;

LiuYanForm.js

import React from 'react';

class LiuYanForm extends React.Component{
   tijiao(event){
        event.preventDefault();
    }
    add(event){        
        if(event.type=="keyup"&&event.keyCode!=13){
            return false;
        }
        let txt=this.refs.txt.value;
        if(txt=="") return false;       
        this.props.addItem(txt);
        this.refs.txt.value="";
    }
    render(){
        return(
             <form className="form-horizontal" onSubmit={this.tijiao.bind(this)}>
                <div className="form-group">
                    <div className="col-sm-10">
                        <input ref="txt"  type="text" className="form-control" onKeyUp={this.add.bind(this)} id="exampleInputName2" placeholder="請輸入留言內容"/>
                    </div>
                    <div className="col-sm-2">
                      <button type="button" className="btn btn-primary" onClick={this.add.bind(this)}>留言</button>
                    </div>               
                </div>           
            </form>
        );
    }
}
export default LiuYanForm;

LiuYanItem.js

import React from 'react';

class LiuYanItem extends React.Component{
    delete(){
        this.props.deleteItem(this.props.data.id);
    }
    render(){

        let {text,time,done,id}=this.props.data;

        return (
           <tr>
               <td>{text}
                   <br/>
                   <br/>
               {time}
               </td>
               <td>
                   <a className="btn glyphicon glyphicon-remove btn-danger" onClick={this.delete.bind(this)}>刪除留言</a>
                </td>
           </tr>
        );
    }
}

export default LiuYanItem;

LiuYanList.js

import React from 'react';

import LiuYanItem from './LiuYanItem';
class LiuYanList extends React.Component{
    render(){
        let todos=this.props.data;
       
        let todoItems=todos.map(item=>{
            return <LiuYanItem deleteItem={this.props.deleteItem} key={item.id} data={item}/>
        });
        
        return (
            <table className="table table-striped">
                <thead>
                    <tr>
                        <th>評論評論</th>                       
                    </tr>
                </thead>
                <tbody>
                    {todoItems}
                </tbody>              
            </table>
        );
    }
}

export default LiuYanList;
相關文章
相關標籤/搜索