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和Vue一样的,核心思想玩的就是组件...
前言: 前段时间学习完react后,刚好就接到公司一个react项目...
前言: 最近收到组长通知我们项目组后面新开的项目准备统一技...
react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom...