create-react-app留言板
create-react-app留言板
搭建环境
创建package.json文件
npm init -y
安装create-react-app
npm install -g create-react-app
使用命令创建myapp目录
create-react-app myapp
进入myapp目录
cd myapp
运行
npm start
它会自动跳转到页面:
安装loder
npm install sass-loader node-sass --save-dev
修改项目
删除不需要的项目
修改完以后的项目目录
编写程序
因为我们这里会用到Bootstarp,所以我们需要先下载一个样式库;
下载Bootstarp样式库
官网网址:
http://v3.bootcss.com/getting-started/#download
直接在浏览器打开就可以,打开以后会出现以下页面,点击第一个,下载Bootstrap就可以;
创建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;
创建webpack.config.js文件
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" } //添加]}
}
输入命令,运行页面
npm start
现在我们的页面就完成了,现在的目录:
运行页面
接下来就让我们看看效果吧!
create-react-app留言板相关推荐
- 【翻译】基于 Create React App路由4.0的异步组件加载(Code Splitting)
基于 Create React App路由4.0的异步组件加载 本文章是一个额外的篇章,它可以在你的React app中,帮助加快初始的加载组件时间.当然这个操作不是完全必要的,但如果你好奇的话,请随 ...
- 在 .NET Core 5 中集成 Create React app
翻译自 Camilo Reyes 2021年2月22日的文章 <Integrate Create React app with .NET Core 5> [1] 本文演示了如何将 Crea ...
- 如何使用Create React App DevOps自动化工作中所有无聊的部分
by James Y Rauhut 詹姆士·鲁豪(James Y Rauhut) 如何使用Create React App DevOps自动化工作中所有无聊的部分 (How I automate al ...
- Create React App 2.0 华丽登场
贺! Create React App 2.0 在 10/02 正式发布 ?????? Create React App 是由官方所维护的开发工具,主要提供了专属于 React 开发环境的前置工作.简 ...
- Create React App 2.0 正式发布:Babel 7、webpack 4 等
英文原文:Create React App 2.0: Babel 7, Sass, and More – React Blog 以下是简要翻译 React 本身是没有附带任何编译工具的,如果没有测试运 ...
- Create React App proxy配置
最近又吃了没文化的亏(哭晕在洗手间≧ ﹏ ≦) 做后台管理系统的时候,需要调一个易连云打印小票的接口, 项目框架是 Create React App+typescript,后端是node.js 本媛负 ...
- 深入浅出 Create React App
本文差点难产而死.因为总结的过程中,多次怀疑本文是对官方文档的直接翻译和简单诺列:同时官方文档很全面,全范围的介绍无疑加深了写作的心智负担.但在最终的梳理中,发现走出了一条与众不同的路,于是坚持分享出 ...
- Vite 3.0 vs. Create React App:比较和迁移指南
Create React App (CRA) 在 React 社区及其整个生态系统的发展中发挥了关键作用. 在突发奇想构建本地 React 开发环境时,它是不同技能的开发人员的首选工具. CRA 拥有 ...
- Create React App从使用到Docker部署
一.介绍 Create React App 是 React 脚手架,它帮我们可以快速生成项目的工程化结构,脚手架让项目从搭建到开发再到部署,整个流程变得快速和便捷. 二.安装 Create React ...
- react快速开始(二)-使用脚手架Create React App创建react应用
文章目录 react快速开始(二)-Create React App入门 什么是Create React App 快速开始 使用IDE webstrom创建react项目 create react a ...
最新文章
- opencv转pytorch
- 使用hibernate自动生成数据库表
- 使用之location和rewrite用法
- python红色的颜色表达式_50行Python代码实现视频中物体颜色识别和跟踪(必须以红色为例)...
- Servlet 是线程安全的吗?
- 浅谈企业MES与ERP系统集成
- UWP通过机器学习加载ONNX进行表情识别
- poj1753_flipgame_枚举
- 奇瑞a3中控按键图解_实拍奇瑞全新瑞虎e 十万元级纯电SUV新选择
- [Teamcenter 2007 开发系列] web 非空验证
- H.248-Transcoding与Interception
- cloudflare免费证书_国外Cloudflare免费ssl证书设置
- Java Web-----轮播图的实现
- xcode instrument profile的坑
- exoplay切换全屏_02.视频播放器整体结构
- Spark数据倾斜优化
- Cocos Creator SDK接入OPPO篇
- SQL语句查询重复语句并进行标记和更新
- Word文档中不显示图片(只有框)
- vue核心面试题:v-for中为什么要用key
热门文章
- [学习笔记]Segment Tree Beats!九老师线段树
- transition过渡规定慢速开始,然后变快,然后慢速结束的过渡效果(cubic-bezier(0.25,0.1,0.25,1))。...
- 疯狂Spring Cloud连载(5)Eureka集群搭建
- 构建虚拟工控环境系列 - 西门子虚拟PLC
- Apache Solr入门教程(初学者之旅)
- MySQL5.7.10多元复制功能搭建
- Python 格式化输出 ( 颜色 )
- 初级篇第六期:学习UITableView
- [转载]Emmet使用手册
- C#语法糖(Csharp Syntactic sugar)大汇总