create-react-app

前端之家收集整理的这篇文章主要介绍了create-react-app前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

作任何东西之前需要搭建环境

首先:安装create-react-app

npm install -g create-react-app

使用命令创建myapp目录

create-react-app myapp

下一步:进入目录命令

cd myapp

运行

npm start

它会自动跳转到浏览器
跳转后再安装命令

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

上面就是环境搭建
环境搭建好后还需要下载bootstarp

可以进去官网直接去下载bootstarp

也可以参考博客参考博客

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;

猜你在找的React相关文章