使用React实现一个TodoList案例
2024-09-01 14:37:33
1.效果图:
2.项目源码
3.源码
TodoList.js
import React, { Component, Fragment } from 'react'; import TodoItem from './TodoItem' import './style.css' //定义一个React组件
class TodoList extends Component { constructor(props) {
super(props);
this.state = {
list: [],
inputValue: '',
} this.handleInputChange = this.handleInputChange.bind(this);
this.handleBtnClick = this.handleBtnClick.bind(this);
this.handleDelete = this.handleDelete.bind(this);
} handleBtnClick() {
if (this.state.inputValue == '') {
alert("请输入代办项内容");
return;
}
this.setState({
list: [...this.state.list, this.state.inputValue],
inputValue: '',
});
} handleInputChange(e) {
this.setState({
inputValue: e.target.value
});
//console.log(e.target.value)
} handleDelete(index) {
const list = [...this.state.list];
list.splice(index, 1);
this.setState({
list: list
})
} getTodoItems() {
return (
this.state.list.map((item, index) => {
return (
<TodoItem
delete={this.handleDelete}
key={index}
content={item}
index={index}
/>)
})
)
} render() {
return (
<div className='w'>
<div className="header">
<div className="header-content">
<span className='logo'>ToDoList</span>
<div className="addItem">
<input placeholder="添加ToDo" value={this.state.inputValue} onChange={this.handleInputChange} />
<button onClick={this.handleBtnClick}>add</button>
</div>
</div>
</div>
<ul className="main">
<div className="main-header">
<span>正在进行</span>
</div>
<div className="main-content">
{this.getTodoItems()}
</div>
<div className="main-header">
<span>已经完成</span>
</div>
</ul>
<div className="footer">
<span>Created by Elylic @2020-9 todolist.cn</span>
</div>
</div>
);
}
} //导出组件
export default TodoList;
TodoItem.js
import React ,{Component} from 'react'
import './item.css' class TodoItem extends Component{ constructor(props){
super(props);
this.handleDelete = this.handleDelete.bind(this)
} handleDelete(){
this.props.delete(this.props.index);
} render(){
const {content } = this.props;
return (
<div>
<p className="todo-item"><input type="checkbox"/>{content}
<button className="todo-delete" onClick={this.handleDelete}>delete</button>
</p>
</div>
)
}
} export default TodoItem;
index.js
/**
* 整个项目的入口文件
*/
//引入React库,理解语法
import React from 'react';
//引入ReactDom让我们可以将一个组件挂载到DOM组件
import ReactDOM from 'react-dom';
//App组件,大写字母开头
import TodoList from './TodoList'; //将组件渲染到页面上
ReactDOM.render(<TodoList />,document.getElementById('root'));
style.css
.w{
width: 100%;
background-color: #c7c6c7;
padding: 0;
margin: 0;
} /*头部*/
.header{
width: 100%px;
height: 50px;
margin: 0 auto;
background-color:#2D2c2D
}
.header-content{
width: 500px;
margin: 0 auto;
}
.logo{
display: inline-block;
float: left;
color: #D9D4D4;
font-size: 26px;
font-weight: bold;
line-height: 20px;
padding-top: 15px;
} .addItem{
float: right;
margin-left: 100px;
padding-top: 15px;
} .addItem input{
display: inline-block;
width: 200px;
height: 20px;
border-radius: 5px;
padding: 0px 10px;
font-size: 12px;
}
.addItem button{
background: #ffffff;
color:#2D2c2D;
border-radius: 5px;
} /*主体内容*/
.main{
width: 500px;
padding:0;
margin: 0 auto;
background-color: #c7c6c7;
} .main-header span{
display: inline-block;
font-size: 20px;
font-weight: bold;
margin: 20px 0;
} /*底部区域*/
.footer{
width: 300px;
padding:0;
margin: 0 auto;
text-align: center;
font-size: 12px;
color:#78777A;
background-color: #c7c6c7;
padding-bottom: 12px;
}
item.css
.todo-item{
display: block;
width: 480px;
background-color: #ffffff;
border-radius: 3px;
padding: 5px 10px;
font-size: 16px;
margin: 5px 0h;
} .todo-item input{
float: left;
margin-right: 10px;
} .todo-delete{
float: right;
color:#2D2c2D;
}
更多参考链接:https://blog.csdn.net/sinat_38368658/article/details/108732416
最新文章
- Hadoop学习笔记—20.网站日志分析项目案例(二)数据清洗
- AndroidStudio NDK配置使用以及错误集合
- python3.5-day5_迭代器_生成器_装饰器_模块
- Python3 学习第十弹: 模块学习三之数字处理
- Block、委托、回调函数原理剖析(在Object C语境)——这样讲还不懂,根本不可能!
- Hudson的安装配置
- vmtouch - the Virtual Memory Toucher
- 4种CSS3效果(360度旋转、旋转放大、放大、移动)
- C++中内存泄露的检测
- Codeforces Round #429 (Div. 2) 补题
- Spring AOP中级——应用场景
- python使用h5py读取mat文件数据,并保存图像
- Easyui datagrid 设置内容超过单元格宽度时自动换行显示
- ykit入门
- python3字典中items()和python2.x中iteritems()有什么不同?
- Educational Codeforces Round 1
- (Arrays.sort() 或 map) Ignatius and the Princess IV hdu1029
- MyBatis第一个案例-----永远的HelloWorld 含所有代码
- 《Android进阶之光》--RxJava
- Oracle监听程序未启动或数据库服务未注册到该监听