React有三種安裝的方式,想了解的登陸React官網查看,今天介紹的一個自動安裝的不須要怎麼配置環境,自動生成的方式,相似於vue-cli!並實現一個小案例,留言功能!
css
官網的安裝教程!vue
//依次安裝 npm install -g create-react-app create-react-app my-app cd my-app npm start
最終啓動自後會出現一個Welcome React頁面,
你們能夠去試試.node
下邊咱們配置一下項目結構react
npm file-loader url url-loader --save-dev
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" } ]
咱們加以改造webpack
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" } //添加 ] } }
{ "name": "myapp", "version": "0.1.0", "private": true, "dependencies": { "antd-mobile": "^1.5.0", "react": "^15.6.1", "react-dom": "^15.6.1", "react-scripts": "1.0.10" }, "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test --env=jsdom", "eject": "react-scripts eject" }, "devDependencies": { "babel-plugin-import": "^1.2.1", "node-sass": "^4.5.3", "sass-loader": "^6.0.6" } }
npm install
咱們的bootstap是事先安裝完成以後,把他移動到css項目目錄下的,index配置以下web
//入口文件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")); //輸出到頁面
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;
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;
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;
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;
這樣咱們的留言功能就建立完了,你們輸入npm start就能夠對項目進行啓動!想了解的同窗們能夠複製回去實驗一下!vue-cli