目录:
public ---- 静态资源文件夹
favicon.icon ------ 网站页签图标
index.html -------- 主页面
logo192.png ------- logo图
logo512.png ------- logo图
manifest.json ----- 应用加壳的配置文件
robots.txt -------- 爬虫协议文件
src ---- 源码文件夹
App.css -------- App组件的样式
App.js --------- App组件
App.test.js ---- 用于给App做测试
index.css ------ 样式
index.js ------- 入口文件
logo.svg ------- logo图
reportWebVitals.js --- 页面性能分析文件(需要web-vitals库的支持)
setupTests.js ---- 组件单元测试的文件(需要jest-dom库的支持)
Hello.jsx
import React, {Component} from 'react'
import './Hello.css'
export default class Hello extends Component {
render() {
return hello,react!
}
}
Hello.css
.title {
background-color: orange;
}
Welcome.jsx
import React, {Component} from "react";
import './Welcome.css'
export default class Welcome extends Component {
render() {
return welcome
}
}
Welcome.css
.demo {
background-color: skyblue;
}
App.js
import React from 'react'
import Hello from "./components/Hello/Hello";
import Welcome from "./components/Welcome/Welcome";
export default class App extends React.Component {
render() {
return (
)
}
}
index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render( );
运行结果:
Hello.jsx
import React, {Component} from 'react'
import hellocss from './Hello.module.css'
export default class Hello extends Component {
render() {
return hello,react!
}
}
Hello.module.css
.title {
background-color: orange;
}
Welcome.jsx
import React, {Component} from "react";
import welcomecss from './Welcome.module.css'
export default class Welcome extends Component {
render() {
return welcome
}
}
Welcome.module.css
.title {
background-color: skyblue;
}
运行结果:
todoList案例相关知识点
Footer.css
/*footer*/
.todo-footer {
height: 40px;
line-height: 40px;
padding-left: 6px;
margin-top: 5px;
}
.todo-footer label {
display: inline-block;
margin-right: 20px;
cursor: pointer;
}
.todo-footer label input {
position: relative;
top: -1px;
vertical-align: middle;
margin-right: 5px;
}
.todo-footer button {
float: right;
margin-top: 5px;
}
Footer.jsx
import React, {Component} from 'react';
import './Footer.css'
class Footer extends Component {
handleCheckAll = (event) => {
this.props.checkAllTodo(event.target.checked)
}
handleClearAllDone = () => {
this.props.clearAllDone()
}
render() {
const {todos} = this.props
const doneCount = todos.reduce((pre, current) => {
return pre + (current.done ? 1 : 0)
}, 0)
const total = todos.length
return (
已完成{doneCount} / 全部{total}
);
}
}
export default Footer;
Header.css
/*header*/
.todo-header input {
width: 560px;
height: 28px;
font-size: 14px;
border: 1px solid #ccc;
border-radius: 4px;
padding: 4px 7px;
}
.todo-header input:focus {
outline: none;
border-color: rgba(82, 168, 236, 0.8);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
}
Header.jsx
import React, {Component} from 'react';
import PropTypes from 'prop-types'
import {nanoid} from "nanoid";
import './Header.css'
class Header extends Component {
static propTypes = {
addTodo: PropTypes.func.isRequired
}
handleKeyUp = (event) => {
const {keyCode, target} = event
if (keyCode != 13) return
if (target.value.trim() == '') {
alert('输入不能为空')
return;
}
const todoObj = {id: nanoid(), name: target.value, done: false}
this.props.addTodo(todoObj)
target.value = ''
}
render() {
return (
);
}
}
export default Header;
Item.css
/*item*/
li {
list-style: none;
height: 36px;
line-height: 36px;
padding: 0 5px;
border-bottom: 1px solid #ddd;
}
li label {
float: left;
cursor: pointer;
}
li label li input {
vertical-align: middle;
margin-right: 6px;
position: relative;
top: -1px;
}
li button {
float: right;
display: none;
margin-top: 3px;
}
li:before {
content: initial;
}
li:last-child {
border-bottom: none;
}
Item.jsx
import React, {Component} from 'react';
import './Item.css'
class Item extends Component {
state = {mouse: false}
handelMouse = (flag) => {
return () => {
this.setState({mouse: flag})
}
}
handleCheck = (id) => {
return (event) => {
this.props.updateTodo(id, event.target.checked)
}
}
handleDelete = (id) => {
if (window.confirm('确定删除吗?')) {
this.props.deleteTodo(id)
}
}
render() {
const {id, name, done} = this.props
const {mouse} = this.state
return (
);
}
}
export default Item;
List.css
/*main*/
.todo-main {
margin-left: 0px;
border: 1px solid #ddd;
border-radius: 2px;
padding: 0px;
}
.todo-empty {
height: 40px;
line-height: 40px;
border: 1px solid #ddd;
border-radius: 2px;
padding-left: 5px;
margin-top: 10px;
}
List.jsx
import React, {Component} from 'react';
import PropTypes from "prop-types";
import Item from "../Item/Item";
import './List.css'
class List extends Component {
static propTypes = {
todos: PropTypes.array.isRequired,
updateTodo: PropTypes.func.isRequired,
deleteTodo: PropTypes.func.isRequired
}
render() {
const {todos, updateTodo, deleteTodo} = this.props
return (
{
todos.map((todo) => {
return
})
}
);
}
}
export default List;
App.css
/*base*/
body {
background: #fff;
}
.btn {
display: inline-block;
padding: 4px 12px;
margin-bottom: 0;
font-size: 14px;
line-height: 20px;
text-align: center;
vertical-align: middle;
cursor: pointer;
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
border-radius: 4px;
}
.btn-danger {
color: #fff;
background-color: #da4f49;
border: 1px solid #bd362f;
}
.btn-danger:hover {
color: #fff;
background-color: #bd362f;
}
.btn:focus {
outline: none;
}
.todo-container {
width: 600px;
margin: 0 auto;
}
.todo-container .todo-wrap {
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
}
App.js
import React, {Component} from 'react';
import './App.css'
import Header from "./components/Header/Header";
import Footer from "./components/Footer/Footer";
import List from "./components/List/List";
class App extends Component {
//初始化状态
state = {
todos: [
{id: '001', name: '吃饭', done: true},
{id: '002', name: '睡觉', done: true},
{id: '003', name: '打代码', done: false},
{id: '004', name: '逛街', done: true},
]
}
addTodo = (todoObj) => {
const {todos} = this.state
const newTodos = [todoObj, ...todos]
this.setState({todos: newTodos})
}
updateTodo = (id, done) => {
const {todos} = this.state
const newTodos = todos.map((todoObj) => {
if (todoObj.id === id) {
return {...todoObj, done: done}
} else {
return todoObj
}
})
this.setState({todos: newTodos})
}
deleteTodo = (id) => {
const {todos} = this.state
const newTodos = todos.filter((todoObj) => {
return todoObj.id !== id
})
this.setState({todos: newTodos})
}
checkAllTodo = (done) => {
const {todos} = this.state
const newTodos = todos.map((todoObj) => {
return {...todoObj, done: done}
})
this.setState({todos: newTodos})
}
clearAllDone = () => {
const {todos} = this.state
const newTodos = todos.filter((todoObj) => {
return !todoObj.done
})
this.setState({todos: newTodos})
}
render() {
return (
);
}
}
export default App;
index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render( );
项目结构:
运行结果: