React結合Bootstrap的使用css
Bootstrap你們應該都不陌生吧,它是一套用於HTML、CSS 和JS的框架,這裏,咱們要使用它裏面的一套樣式框架;html
官網網址: http://v3.bootcss.com/getting-started/#download 直接在瀏覽器打開就能夠,打開之後會出現如下頁面,點擊第一個,下載Bootstrap就能夠;
在命令行裏輸入 npm install file-loader url url-loader --save-dev
module.exports = { entry: './index.js', output: { path:path.resolve(__dirname,"build"), publicPath:"/assets/", filename: 'bundle.js' }, module: { rules: [ { test: /\.js$/, exclude: /node_modules/, loader: "react-hot-loader!babel-loader" }, { test: /\.css$/, exclude: /node_modules/, loader: "style-loader!css-loader" }, { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader" }, //添加 { test: /\.(woff|woff2)$/, loader:"url-loader?prefix=font/&limit=5000" }, //添加 { test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: "url-loader?limit=10000&mimetype=application/octet-stream" }, //添加 { test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: "url-loader?limit=10000&mimetype=image/svg+xml" } //添加 ] } }
這樣咱們的環境就搭建完成了;node
我作了一個用React和Bootstarp的一個例子,一個提交的表格,下面我來給你們詳細的介紹一下:react
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>React3</title> </head> <body> <div id="app"></div> <script src="/assets/bundle.js"></script> </body> </html>
import React模塊 from 'react'; //導入React模塊 import ToDoList from './ToDoList'; //導入ToDoList模塊 import ToDoForm from './ToDoForm'; //導入ToDoForm模塊 class ToDoapp 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); this.okItem=this.okItem.bind(this); } okItem(id){ this.state.todos.map(item=>{ if(item.id==id){ item.done=1; } return item; }); this.setState({ todos:this.state.todos }); } 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/> <div className="panel panel-default"> <div className="panel-headingbg-danger"> <h1 className="text-center ">ToDo<small>你要作什麼?</small></h1> <hr/> </div> <div className="panel-body"> <ToDoForm addItem={this.addItem}/> <ToDoList okItem={this.okItem} deleteItem={this.deleteItem} data={this.state.todos}/> </div> </div> </div> ); } } export default ToDoapp; //導出ToDoapp模塊
import React from 'react'; import ToDoItem from './ToDoItem'; //導入ToDoItem模塊 class ToDoList extends React.Component{ render(){ let todos=this.props.data; let todoItems=todos.map(item=>{ return <ToDoItem okItem={this.props.okItem} deleteItem={this.props.deleteItem} key={item.id} data={item}/> }); return ( <table className="table table-striped"> <thead> <tr> <th>內容</th> <th>時間</th> <th>狀態</th> <th>操做</th> </tr> </thead> <tbody> {todoItems} </tbody> </table> ); } } export default ToDoList; //導出ToDoList模塊
import React from 'react'; class ToDoItem extends React.Component{ delete(){ this.props.deleteItem(this.props.data.id); } complete(){ this.props.okItem(this.props.data.id); } render(){ let {text,time,done,id}=this.props.data; return ( <tr> <td>{text}</td> <td>{time}</td> <td>{done==0?"未完成":"完成"}</td> <td> <a className="btn btn-primary" onClick={this.delete.bind(this)}>刪除</a> <a className="btn btn-success" onClick={this.complete.bind(this)}> <span className="glyphicon glyphicon-ok" aria-hidden="true"></span> 完成 </a> </td> </tr> ); } } export default ToDoItem; //導出ToDoItem模塊
import React from 'react'; class ToDoForm 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 ToDoForm; //導出ToDoForm模塊
這裏咱們能用到了柵格化佈局;webpack
接下來就讓咱們來看一下效果吧:web
效果:添加的時候能夠顯示當前時間,安回車鍵就能夠直接添加,點擊完成能夠把未完成改爲完成,點擊刪除能夠刪除內容;npm