使用react实现一个简版的印象笔记App

一、效果图展示

二、初始化项目

① 安装react脚手架工具

// 全局安装 create-react-app
sudo npm -g install create-react-app
// 通过create-react-app命令创建react项目
create-react-app evernote

除了通过全局安装create-react-app来创建react项目外,我们还可以通过npm init命令去创建,即

npm init react-app evernote

因为执行npm init命令的时候,会自动在init之后的包名加上create前缀,所以相当于安装并执行create-react-app包,所以我们再传入项目名称即可创建对应的react项目了。注意,react的项目名不能以大写字母开头

② 修改public下的index.html文件
react项目和vue项目一样也是单页面应用,所以public目录下也会有一个index.html页面,用于挂载react渲染的结果。由于我们的项目中会用到一些字体图标,所以我们需要把我们的字体链接对应的css引入进来,如:

// public/index.html
<!DOCTYPE html>
<html lang="en">
    <head>
         <link rel="stylesheet" href="//at.alicdn.com/t/font_1609262_ijm97o315o.css"/>
    </head>
</html>

这个在线css链接可以到阿里巴巴iconfont字体库中找到,如图所示:

③ 修改src/App.js文件
src/App.js是项目的根组件,我们将App.js中的代码删除,默认App是函数组件,我们这里改成类组件,因为我们需要让App组件拥有自己的状态,然后修改为如下代码:

import React from 'react';
import './App.css';

class App extends React.Component {
  render() {
    return (
      <div className="app-container">
          hello evernote.
      </div>
    );
  }
}
export default App;

④ 修改src/App.css
将原来的App.css内容清空,这里样式就不做过多解释,然后修改如下:

.app-container {
  display: flex;
  height: 100%;
}
.app-left {
  width: 10%;
  min-width: 190px;
  background: #343434;
}
.app-left-header, .app-left-body-title {
  display: flex;
  align-items: center;
  color: white;
  font-weight: bold;
  padding: 10px 0;
}
.add {
  width: 25px;
  height: 25px;
  display: inline-block;
  border-radius: 50%;
  margin: 0 10px;
  background:#6fcb66;
  text-align: center;
  line-height: 25px;
  font-size: 15px;
  font-weight: bold;
}
.notebook-icon {
  height: 25px;
  text-align: center;
  line-height: 25px;
  margin: 0 5px 0 10px;
}
.notebook-list, .app-center-list{
  margin: 0;
  list-style: none;
  padding: 0;
  color: white;
}
.notebook-list li {
  margin-top: 10px;
  font-size: 14px;
  display: flex;
  padding: 5px 0  5px 25px;
}
.notebook-list .active {
  background: #1a1a1a;
}
.notebook-list li i {
  margin-right: 3px;
}
.app-center{
  width: 12%;
  min-width: 180px;
  background: #ececec;
  display: flex;
  flex-direction: column;
}
.app-center-header {
  padding: 10px 0px 10px 10px;
  font-weight: bold;
  border-bottom: 1px solid #ccc;
}
.app-center-list {
  padding: 0;
  height: 100%;
  overflow: scroll;
}
.app-center-list .active {
  background: yellow;
}
.app-center-list li {
  margin: 5px 10px;
  height: 180px;
  background: white;
  color: black;
}
.app-center-list-item .note-header {
  font-weight: bold;
  text-align: center;
  height: 30px;
  line-height: 30px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  padding-left: 5px;
  font-size: 12px;
}
.app-center-list-item .note-content {
  padding: 0px 10px;
  font-size: 13px;
  line-height: 22px;
  overflow: hidden;
  height: 130px;
  line-clamp: 3;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 6;
}

.app-right {
  width: 78%;
  display: flex;
  flex-direction: column;
}
.app-right-header {
  height: 30px;
  line-height: 30px;
  padding-left: 10px;
  border-bottom: 1px solid #ccc;
}
.app-right-header .notebookName {
  margin-left: 5px;
  font-weight: bolder;
  font-size: 13px;
}
.app-right-title {
  height: 20px;
  line-height: 20px;
  padding: 8px 0 8px 30px;
  font-weight: bold;
  outline: none;
}
.app-right-content {
  display: flex;
  flex: 1;
  overflow: scroll;
}
.app-right-edit {
  background: #22272a;
  width: 50%;
  color: white;
  font-size: 15px;
  line-height: 25px;
  padding: 10px 0 0 10px;
  outline: none;
  resize: none;
}
.app-right-show {
  border-top: 1px solid #ccc;
  width: 50%;
  padding-left: 20px;
  overflow: scroll;
}

这里为了方便,将所有样式都写到了,App.css中,这样其他子组件都可以共享App.css中定义的样式了。

④ 修改index.css
为了让App组件能够占满全屏,需要对html、body、#root进行高度100%的设置,如:

html,body{
    height: 100%;
}
#root {
  height: 100%;
}

至此,evernote项目已经初始化完成。

三、项目分析

从效果图上可以看到,整个印象笔记分为左、中、右三块&#

  • 10
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是一个简单的购物车功能的实现: 1. 创建一个商品组件,包括商品名称、价格、图片等信息,并提供一个添加到购物车的按钮。 import React from 'react'; const Product = ({ name, price, image, onAddToCart }) => { return ( <div className="product"> <img src={image} alt={name} /> <h2>{name}</h2> <p>{price}</p> <button onClick={onAddToCart}>Add to Cart</button> </div> ); }; export default Product; 2. 创建一个购物车组件,包括购物车中的商品列表和总价。 import React from 'react'; const ShoppingCart = ({ cartItems }) => { const totalPrice = cartItems.reduce((total, item) => total + item.price, 0); return ( <div className="shopping-cart"> <h2>Shopping Cart</h2> <ul> {cartItems.map(item => ( <li key={item.id}> {item.name} - {item.price} </li> ))} </ul> <p>Total Price: {totalPrice}</p> </div> ); }; export default ShoppingCart; 3. 创建一个App组件,包括商品列表和购物车组件,并处理添加到购物车的逻辑。 import React, { useState } from 'react'; import Product from './Product'; import ShoppingCart from './ShoppingCart'; const App = () => { const [cartItems, setCartItems] = useState([]); const handleAddToCart = (product) => { setCartItems([...cartItems, product]); }; return ( <div className="app"> <div className="product-list"> <Product name="Product 1" price="10.00" image="https://via.placeholder.com/150" onAddToCart={() => handleAddToCart({ id: 1, name: 'Product 1', price: 10 })} /> <Product name="Product 2" price="20.00" image="https://via.placeholder.com/150" onAddToCart={() => handleAddToCart({ id: 2, name: 'Product 2', price: 20 })} /> <Product name="Product 3" price="30.00" image="https://via.placeholder.com/150" onAddToCart={() => handleAddToCart({ id: 3, name: 'Product 3', price: 30 })} /> </div> <ShoppingCart cartItems={cartItems} /> </div> ); }; export default App; 这样就可以通过点击商品的添加到购物车按钮将商品加入购物车,并在购物车中显示出来。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈瀛Neptune

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值