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 数据库: ...
随机推荐
- 11-09SQLserver 基础-数据库之汇总练习45题
设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表 ...
- No result defined for action action.LoginAction and result success 问题解决
转自:https://blog.csdn.net/dongzhout/article/details/43699699 搭建好SSH2框架,写一个简单的登陆功能,提交表单的时候遇到这个问题: 配置文件 ...
- Eclipse中如何开启断言(Assert),方法有二
Eclipse中如何开启断言(Assert),方法有二:1.Run -> Run Configurations -> Arguments页签 -> VM arguments文本框中加 ...
- queue队列模块
import Queue myqueue = Queue.Queue(maxsize = 10) Queue.Queue类即是一个队列的同步实现.队列长度可为无限或者有限.可通过Queue的构造函数的 ...
- [hdu1251]统计难题(trie模板题)
题意:返回字典中所有以测试串为前缀的字符串总数. 解题关键:trie模板题,由AC自动机的板子稍加改造而来. #include<cstdio> #include<cstring> ...
- Angular04 组件动态地从外部接收值、在组件中使用组件
一.组件从外部接收值 1 修改组件的ts文件,让组件的属性可以从外部接收值 1.1 导入Input注解对象 1.2 在属性变量前面添加 @Input() 注解 1.3 去掉构造器中的属性变量赋值语句 ...
- 算法Sedgewick第四版-第1章基础-020一按优先级计算表达式的值
/****************************************************************************** * Compilation: javac ...
- java的类型转换问题。int a = 123456;short b = (short)a;System.out.println(b);为什么结果是-7616?
这个从二进制来解释: int 是 32 位,也就是最多能表示长度为 32 位的二进制整数.第一位是符号位,表示正负,0 表示正的,1 表示负的.当首位为 1(为负)时,把所有的位取反(0 变成 1,1 ...
- Luogu 3168 [CQOI2015]任务查询系统
区间修改单点查询,又观察到是一个k小,考虑主席树上做差分 一开始样例疯狂挂,后来发现主席树在一个历史版本上只能修改一次,所以要开2*n个根结点,记录一下每个时间对应的根结点编号 然后80分,考虑到当一 ...
- Entity Framework Tutorial Basics(32):Enum Support
Enum in Entity Framework: You can now have an Enum in Entity Framework 5.0 onwards. EF 5 should targ ...