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代码优化的更多相关文章

  1. react todolist

    import React, {Component} from 'react'; class AddItem extends React.Component { constructor(props) { ...

  2. React ToDolist增加功能

    补充知识点1==>npm install prop-types 先安装参数校验包 在B C页面引入 import PropTypes from 'prop-types' //参数限制 // 验证 ...

  3. 【转载】React入门-Todolist制作学习

    我直接看的这个React TodoList的例子(非常好!): http://www.reqianduan.com/2297.html 文中示例的代码访问路径:http://127.0.0.1:708 ...

  4. Node.js + React + MongoDB 实现 TodoList 单页应用

    之前用 Ant Design 开发了一个项目,因此对 React 的特性有了一定的了解,React 使用封装组件的思想,组件各自维护自己的状态和 UI, 组件之间通过 props 传递数据和方法.当状 ...

  5. 一个简单的 react 实例: < TodoList >

    <  react     TodoList:  > 组件: //引入React : import React from 'react'; //组件 class TodoList exten ...

  6. (三)React基础

    3-1 使用React编写TodoList功能 import { Fragment} from ‘react’ Fragment是占位符 用于替代最外层div元素, 防止生成的元素会有两层div嵌套这 ...

  7. (2)React的开发

    实例: import React from 'react'; class TodoList extends React.Component { constructor(props){ super(pr ...

  8. 详解 Node + Redux + MongoDB 实现 Todolist

    前言 为什么要使用 Redux? 组件化的开发思想解放了繁琐低效的 DOM 操作,以 React 来说,一切皆为状态,通过状态可以控制视图的变化,然后随着应用项目的规模的不断扩大和应用功能的不断丰富, ...

  9. React开发实时聊天招聘工具 -第一章

    第一章 课程道学 6个页面 弱化css Antd-mobile作为组件库 Redux 状态管理 React-Router 路由 Axios异步请求 后端Express框架 Socket.io 数据库: ...

随机推荐

  1. 11-02SQLserver基础--字符串函数

    数据库の函数 一.内置函数--字符串函数 1.--ASCII 返回字符串的首字母的ASCII编码 select ASCII('ame') select ASCII(xingming)from xues ...

  2. Http 与Https

    一个Http请求 DNS域名解析 --> 发起TCP的三次握手 --> 建立TCP连接后发起http请求 --> 服务器响应http请求,浏览器得到html代码 --> 浏览器 ...

  3. 0016_练习题d2

    __author__ = 'qq593' #!/usr/bin/env python #-*- coding:utf-8 -*- #元素分类,有如下值集合[11,22,33,44,55,66,77,8 ...

  4. myeclipse.ini

    myeclipse10 32位 我的配置 #utf8 (do not remove) #utf8 (do not remove) -startup ../Common/plugins/org.ecli ...

  5. ListView里面adapter的不同分类的item

    public class PlayAdapter extends BaseAdapter { /** * 标题的item */ public static final int ITEM_TITLE = ...

  6. linq to sql 不能更新的问题

    今天在项目中用linq更新一个表的时候,结果怎么都更新不了,最蛋疼的是什么异常也不报,发现db.table1.isReadOnly为True 知道问题所在,百度后得到解决办法: 原来是我的表没有增加主 ...

  7. HDOJ 1196 Lowest Bit

    题目大意是给一个1-100的整数,要求首先转化成2进制,然后从最低位开始数起到不是0的位停止,输出这些位代表队额10进制数 #include <iostream> using namesp ...

  8. java中方法的控制修饰符也分为:可访问控制符和非访问控制符两类。

    3 .方法的控制修饰符也分为:可访问控制符和非访问控制符两类. 可访问控制符有 4 种:公共访问控制符: public :私有访问控制符: private :保护访问控制符: protected :私 ...

  9. EZOJ #73

    传送门 分析 我们知道如果对于模数$P$有$gcd(x,P) = 1$则$x$一定有且仅有一个逆元,可以表示为 $x \equiv \frac{y}{1} (mod P)$ 即为$xy \equiv ...

  10. ARC102E Stop. Otherwise...

    传送门 题目大意 现在有n个k面的骰子,问在i=2~2*k的情况下,任意两个骰子向上那一面的和不等于i的方案数是多少. 分析 这道题具体做法见这个博客. 至于k2的值为啥是那个自己画画图就明白了. 代 ...