react todolist代码优化
Todolist.js
import React, { Component,Fragment } from 'react';
import TodoItem from './TodoItem';
import './style.css';
class Todolist extends Component {
constructor(props) { //最优先执行的函数
super(props);
this.state={
inputValue:'',
list:[]
}
this.handleinputChange=this.handleinputChange.bind(this);
this.handlebuttonClick=this.handlebuttonClick.bind(this);
this.handleItemdelt=this.handleItemdelt.bind(this);
}
render() {
return (
<Fragment>
<div>
{/*这是一个todolist*/}
<label htmlFor='insertArea'>输入内容</label>
<input
id="insertArea"
className='input'
value={this.state.inputValue}
onChange={this.handleinputChange}
/>
<button onClick={this.handlebuttonClick}> 提交 </button>
</div>
<ul>
{this.getTodoItem()}
</ul>
</Fragment>
);
}
getTodoItem(){
return this.state.list.map((item,index)=>{
return(
<TodoItem
key={index}
index={ index }
item={ item }
deleteItem={this.handleItemdelt}
/>
)
})
}
handleinputChange(e){
const value=e.target.value;
this.setState(()=>({
inputValue:value
}));
// this.setState({
// inputValue:e.target.value
// })
}
handlebuttonClick(e){
this.setState((prevState)=>{
return{
list:[...prevState.list,prevState.inputValue],
inputValue:''
}
});
// this.setState({
// list:[...this.state.list,this.state.inputValue],
// inputValue:''
// })
}
handleItemdelt(index){
// immutable
// state 不允许我们坐任何的改变
this.setState((prevState)=>{
const list=[...prevState.list];
list.splice(index,1);
return{list}
})
// const list=[...this.state.list]; // list的一个副本
// list.splice(index,1);
// this.setState({
// list:list
// })
}
}
export default Todolist;
TodoItem.js
import React ,{ Component } from 'react';
class TodoItem extends Component{
constructor(props){
super(props);
this.handleclick=this.handleclick.bind(this);
}
render(){
const { item }=this.props;
return (<li
onClick={this.handleclick}
dangerouslySetInnerHTML={{__html:item}}
></li>
)
}
handleclick(){
const {deleteItem,index}=this.props;
deleteItem(index);
}
}
export default TodoItem;
react todolist代码优化的更多相关文章
- react todolist
import React, {Component} from 'react'; class AddItem extends React.Component { constructor(props) { ...
- React ToDolist增加功能
补充知识点1==>npm install prop-types 先安装参数校验包 在B C页面引入 import PropTypes from 'prop-types' //参数限制 // 验证 ...
- 【转载】React入门-Todolist制作学习
我直接看的这个React TodoList的例子(非常好!): http://www.reqianduan.com/2297.html 文中示例的代码访问路径:http://127.0.0.1:708 ...
- Node.js + React + MongoDB 实现 TodoList 单页应用
之前用 Ant Design 开发了一个项目,因此对 React 的特性有了一定的了解,React 使用封装组件的思想,组件各自维护自己的状态和 UI, 组件之间通过 props 传递数据和方法.当状 ...
- 一个简单的 react 实例: < TodoList >
< react TodoList: > 组件: //引入React : import React from 'react'; //组件 class TodoList exten ...
- (三)React基础
3-1 使用React编写TodoList功能 import { Fragment} from ‘react’ Fragment是占位符 用于替代最外层div元素, 防止生成的元素会有两层div嵌套这 ...
- (2)React的开发
实例: import React from 'react'; class TodoList extends React.Component { constructor(props){ super(pr ...
- 详解 Node + Redux + MongoDB 实现 Todolist
前言 为什么要使用 Redux? 组件化的开发思想解放了繁琐低效的 DOM 操作,以 React 来说,一切皆为状态,通过状态可以控制视图的变化,然后随着应用项目的规模的不断扩大和应用功能的不断丰富, ...
- React开发实时聊天招聘工具 -第一章
第一章 课程道学 6个页面 弱化css Antd-mobile作为组件库 Redux 状态管理 React-Router 路由 Axios异步请求 后端Express框架 Socket.io 数据库: ...
随机推荐
- 解决列表中增加字典覆盖之前相同key的字典
dic = {} lst = [] # 先声明一个字典和一个列表 dic['name'] = "chenrun" lst.append(dic) print(lst) dic[&q ...
- C++面向对象类的实例题目十一
题目描述: 写一个程序计算三角形,正方形和圆形3种图形的面积 程序代码: #include<iostream> #include<cmath> #define PAI 3.14 ...
- Win10系统下安装360安全卫士,安装完成后一直提示
新买的电脑,第一次安装360安全卫士,安装完成后一直报这个错误,显示MiniUI.dll文件不存在,但这个文件就在安装目录下:后面有多次卸载.安装都能能成功,求助大神
- R: 正则表达式
正则表达式: 例:sub("a","",c("abcd","dcba")): [1] "bcd" ...
- NCBI下载SRA数据
从NCBI下载数据本来是一件很简单的事情,但是今天碰到几个坑: 1.paper里没有提供SRA数据号.也没有提供路径: 2.不知道文件在ftp的地址,不能直接用wget下载 所以通过在NCBI官网,直 ...
- Luogu 1445 樱花
BZOJ 2721 唔,太菜了弄不来. 先通分:得到 $\frac{x + y}{xy} = \frac{1}{n!}$ 两边乘一下 $(x + y)n! - xy = 0$ 两边加上$(n!)^2$ ...
- niginx隐藏入口文件index.php
location / { if (!-e $request_filename) { rewrite ^/(.*)$ /index.php/$ last; break; } }
- static的功能
static : 翻译成中文是静态的意思. 使用内部函数的好处是:不同的人编写不同的函数时,不用担心自己定义的函数,是否会与其它文件中的函数同名,因为同名也没有关系. 在C语言中,static的 ...
- springcloud 通过后端去下载和预览文件,要重设跨域允许
@RequestMapping("/download") public void downloadNet(String uri, boolean isOnLine, HttpSer ...
- GridView里的按钮事件
http://www.cnblogs.com/insus/archive/2012/09/22/2697862.html using System;using System.Collections.G ...