如何用react做留言板

如何用react做留言板

先看一下我们的留言板,然后再去实现功能

1.jpg-600

做留言板首先要配置好我们的文件,然后才能接着做我们的留言板

快速开始:

npm install -g create-react-app /* 安装create-react-app,建议使用cnpm */
create-react-app myapp       /* 使用命令创建应用,myapp为项目名称 */
cd myapp                 /* 进入目录,然后启动 */
npm start

留言板各个组件及功能描述:

1. 留言板总共4个组件LiuApp.js、LiuList.js、LiuForm.js、LiuItem.js

2. LiuApp组件调用LiuList和LiuForm组件

3. LiuList组件调用LiuItem组件

4. LiuList负责展示每一条留言,LiuItem负责展示留言内容和删除留言。LiuForm用于发布留言。

接下来看看我们的代码吧

index.html

<body>
    <div id="app">
    </div>
    <script src="/assets/bundle.js"></script>
</body>

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import LiuApp from './LiuApp';
import './bootstrap/dist/css/bootstrap.min.css';
ReactDOM.render(<LiuApp/>,document.getElementById("app"));

LiuApp.js

import React from 'react';
import LiuList from './LiuList';
import LiuForm from './LiuForm';
class LiuApp 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">
                      <h1 className="text-center ">留言板</h1>
                      <hr/>
                    </div>
                    <div className="panel-body">
                      <LiuList deleteItem={this.deleteItem}
                       data={this.state.todos}/>
                      <LiuForm addItem={this.addItem}/>
                    </div>
                </div> 
            </div>
          
        );
    }
}
export default LiuApp;

LiuList.js

import React from 'react';
import LiuItem from './LiuItem';
class LiuList extends React.Component{
    render(){
        let todos=this.props.data;
       
        let todoItems=todos.map(item=>{
            return <LiuItem 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 LiuList;

LiuForm.js

import React from 'react';
class LiuForm 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 LiuForm;

LiuItem.js

import React from 'react';
class LiuItem extends React.Component{
    delete(){
        this.props.deleteItem(this.props.data.id);
    }
    render(){
        let {text,time,done,id}=this.props.data;
        return (
           <tr>
               <td>{time}<br/><br/>{text}</td>
                <td>
                    <br/>
                    <br/>
                   <a onClick={this.delete.bind(this)}>删除留言</a>
                </td>
           </tr>
        );
    }
}
export default LiuItem;

以上就是多有的代码,现在看看我们最终的结果

3809749894-597dc3779064e_articlex.gif

本文来自React答疑栏目,欢迎学习!

以上就是如何用react做留言板的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » React 答疑