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 数据库: ...
随机推荐
- NBU客户端备份失败,报错error 48 client hostname could not be found
今天在做备份时发现了这个报错.经过ping, nslookup, bpclntcmd命令检查没有发现连接或域名解析存在问题. 参考文档http://www.symantec.com/docs/TECH ...
- JavaScript中给对象添加方法
在JavaScript中,我们经常要给已定义的对象添加一些方法,如下: function circle(w,h){ this.width=w; this.height=h; ...
- Tiny4412 Uboot
1. Build uboot a) 安装好toolchain (arm-linux-gcc-4.5.1-v6-vfp-20120301.tgz)并设置好 环境变量PATH,保证可以正常使用. b) 解 ...
- 百度地图SDK v2.1.2使用方法
1.开发工具 Android开发工具有很多,开发者可根据自己的喜好进行选择.在此,我们推荐开发者使用Eclipse作为自己的开发工具,本套开发指南也是针对Eclipse开发环境下进行编写的. 2.工程 ...
- js实现鼠标拖拽
主要原理: 1.当鼠标按下时,记录鼠标坐标,用到的是 onmousedown: 2.当鼠标移动时,计算鼠标移动的坐标之差,用到的是 onmousemove: 3.当鼠标松开时,清除事件,用到的是 on ...
- solr的查询语法、查询参数、检索运算符
转载自:http://martin3000.iteye.com/blog/1328931 1.查询语法 solr的一些查询语法 1.1. 首先假设我的数据里fields有:name, tel, add ...
- Tensorflow fetch和feed
import tensorflow as tf #Fetch input1 = tf.constant(1.0)input2 = tf.constant(3.0)input3 = tf.constan ...
- tee 可以看见输出并将其写入到一个文件中
如下使用tee命令在屏幕上看见输出并同样写入到日志文件my.log中: [root@localhost home]# top |tee my.log tee可以保证你同时在屏幕上看到top的 ...
- 业务逻辑: Quartz的整合应用
1. 请谈一下你对Quartz的理解 思路:根据他解决的什么问题方面去阐述 2. 完成quartz和spring的整合应用 思路:触发时间.任务调度工程 步骤: 1. 创建maven工程,并导入qua ...
- SVN资源库报错:Could not create the view: org.tigris.subversion.subclipse.ui.repository.RepositoriesView
解决方法: 关闭正在运行的myeclipse,然后打开myeclipse安装路径(我的安装在c盘): c:\ProgramFiles\MyEclipse\MyEclipse Professional ...