实例:

import React from 'react';

class TodoList extends React.Component {

  constructor(props){
super(props);
this.state = {
list: [
'learn react',
'learn english',
'learn vue'
]
}
} handleBtnClick() {
this.setState({
list: [...this.state.list, 'hello world']
})
this.state.list.push('hello world');
} render() { return (
<div>
<div>
<input/>
<button onClick={this.handleBtnClick.bind(this)}>add</button>
</div>
<ul>
{
this.state.list.map((item) => {
return <li>{item}</li>
})
}
</ul>
</div>
);
} } export default TodoList;

新增列表 项功能

import React from 'react';

class TodoList extends React.Component {

  constructor(props){
super(props);
this.state = {
list: [
],
inputValue: ''
}
} handleBtnClick() {
this.setState({
list: [...this.state.list, this.state.inputValue],
inputValue: ''
})
} handleInputChange(e) {
this.setState({
inputValue: e.target.value
})
} render() { return (
<div>
<div>
<input value={this.state.inputValue} onChange={this.handleInputChange.bind(this)}/>
<button onClick={this.handleBtnClick.bind(this)}>add</button>
</div>
<ul>
{
this.state.list.map((item) => {
return <li>{item}</li>
})
}
</ul>
</div>
);
} } export default TodoList;

删除:

import React from 'react';

class TodoList extends React.Component {

  constructor(props){
super(props);
this.state = {
list: [
],
inputValue: ''
}
} handleBtnClick() {
this.setState({
list: [...this.state.list, this.state.inputValue],
inputValue: ''
})
} handleInputChange(e) {
this.setState({
inputValue: e.target.value
})
} handleItemClick(index) {
const list = [...this.state.list];
list.splice(index, 1);
this.setState({
list: list
})
} render() { return (
<div>
<div>
<input value={this.state.inputValue} onChange={this.handleInputChange.bind(this)}/>
<button onClick={this.handleBtnClick.bind(this)}>add</button>
</div>
<ul>
{
this.state.list.map((item, index) => {
return <li key={index} onClick={this.handleItemClick.bind(this, index)}>{item}</li>
})
}
</ul>
</div>
);
} } export default TodoList;

React中组件

import React from 'react';
import TodoItem from './TodoItem' class TodoList extends React.Component { constructor(props){
super(props);
this.state = {
list: [
],
inputValue: ''
}
} handleBtnClick() {
this.setState({
list: [...this.state.list, this.state.inputValue],
inputValue: ''
})
} handleInputChange(e) {
this.setState({
inputValue: e.target.value
})
} handleItemClick(index) {
const list = [...this.state.list];
list.splice(index, 1);
this.setState({
list: list
})
} render() { return (
<div>
<div>
<input value={this.state.inputValue} onChange={this.handleInputChange.bind(this)}/>
<button onClick={this.handleBtnClick.bind(this)}>add</button>
</div>
<ul>
{
this.state.list.map((item, index) => {
return <TodoItem key={index} content={item}/>
})
}
</ul>
</div>
);
} } export default TodoList;
import React from 'react';

class TodoItem extends React.Component {
render() {
return (
<div>{this.props.content}</div>
)
}
} export default TodoItem;

import React from 'react';
import TodoItem from './TodoItem' class TodoList extends React.Component { constructor(props){
super(props);
this.state = {
list: [
],
inputValue: ''
}
} handleBtnClick() {
this.setState({
list: [...this.state.list, this.state.inputValue],
inputValue: ''
})
} handleInputChange(e) {
this.setState({
inputValue: e.target.value
})
} // handleItemClick(index) {
// const list = [...this.state.list];
// list.splice(index, 1);
// this.setState({
// list: list
// })
// } handleDelete(index) {
const list = [...this.state.list];
list.splice(index, 1);
this.setState({
list: list
}) } render() { return (
<div>
<div>
<input value={this.state.inputValue} onChange={this.handleInputChange.bind(this)}/>
<button onClick={this.handleBtnClick.bind(this)}>add</button>
</div>
<ul>
{
this.state.list.map((item, index) => {
return <TodoItem delete={this.handleDelete.bind(this)} key={index} content={item} index={index}/>
})
}
</ul>
</div>
);
} } export default TodoList;

代码优化:

import React from 'react';
import TodoItem from './TodoItem' class TodoList extends React.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() {
this.setState({
list: [...this.state.list, this.state.inputValue],
inputValue: ''
})
} handleInputChange(e) {
this.setState({
inputValue: e.target.value
})
} // handleItemClick(index) {
// const list = [...this.state.list];
// list.splice(index, 1);
// this.setState({
// list: list
// })
// } 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>
<div>
<input value={this.state.inputValue} onChange={this.handleInputChange}/>
<button onClick={this.handleBtnClick}>add</button>
</div>
<ul>{this.getTodoItems()}</ul>
</div>
);
} } export default TodoList;
import React from 'react';

class TodoItem extends React.Component {

	// 子组件如果想和父组件通信,子组件要调用父组件传递过来的方法

	constructor(props) {
super(props);
this.handleDelete = this.handleDelete.bind(this);
} handleDelete() {
// const { delete, index } = this.props;
// delete(index); this.props.delete(this.props.index);
console.log(this.props.index)
} render() {
const { content } = this.props;
return (
<div onClick={this.handleDelete}>{this.props.content}</div>
)
}
} export default TodoItem;

写css

import React from 'react';
import TodoItem from './TodoItem' class TodoList extends React.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() {
this.setState({
list: [...this.state.list, this.state.inputValue],
inputValue: ''
})
} handleInputChange(e) {
this.setState({
inputValue: e.target.value
})
} // handleItemClick(index) {
// const list = [...this.state.list];
// list.splice(index, 1);
// this.setState({
// list: list
// })
// } 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>
<div>
<input value={this.state.inputValue} onChange={this.handleInputChange}/>
<button className='red-btn' style={{background: 'red', color: '#fff'}} onClick={this.handleBtnClick}>add</button>
</div>
<ul>{this.getTodoItems()}</ul>
</div>
);
} } export default TodoList;

有两层:

import React from 'react';
import TodoItem from './TodoItem' class TodoList extends React.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() {
this.setState({
list: [...this.state.list, this.state.inputValue],
inputValue: ''
})
} handleInputChange(e) {
this.setState({
inputValue: e.target.value
})
} // handleItemClick(index) {
// const list = [...this.state.list];
// list.splice(index, 1);
// this.setState({
// list: list
// })
// } 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 (
<React.Fragment>
<div>
<input value={this.state.inputValue} onChange={this.handleInputChange}/>
<button className='red-btn' onClick={this.handleBtnClick}>add</button>
</div>
<ul>{this.getTodoItems()}</ul>
</React.Fragment>
);
} } export default TodoList;

import React,{Component} from 'react';

class TodoItem extends Component {

	// 子组件如果想和父组件通信,子组件要调用父组件传递过来的方法

	constructor(props) {
super(props);
this.handleDelete = this.handleDelete.bind(this);
} handleDelete() {
// const { delete, index } = this.props;
// delete(index); this.props.delete(this.props.index);
console.log(this.props.index)
} render() {
const { content } = this.props;
return (
<div onClick={this.handleDelete}>{this.props.content}</div>
)
}
} export default TodoItem;
import React, { Component, Fragment } from 'react';
import TodoItem from './TodoItem' 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() {
this.setState({
list: [...this.state.list, this.state.inputValue],
inputValue: ''
})
} handleInputChange(e) {
this.setState({
inputValue: e.target.value
})
} // handleItemClick(index) {
// const list = [...this.state.list];
// list.splice(index, 1);
// this.setState({
// list: list
// })
// } 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 (
<Fragment>
<div>
<input value={this.state.inputValue} onChange={this.handleInputChange}/>
<button className='red-btn' onClick={this.handleBtnClick}>add</button>
</div>
<ul>{this.getTodoItems()}</ul>
</Fragment>
);
} } export default TodoList;
.red-btn {
background: red;
color: #fff;
}
import React from 'react';
import ReactDOM from 'react-dom'; import TodoList from './TodoList';
import './style.css' ReactDOM.render(<TodoList />, document.getElementById('root'));

请点赞!因为你的鼓励是我写作的最大动力!

吹逼交流群:711613774

(2)React的开发的更多相关文章

  1. [webpack] 配置react+es6开发环境

    写在前面 每次开新项目都要重新安装需要的包,简单记录一下. 以下仅包含最简单的功能: 编译react 编译es6 打包src中入口文件index.js至dist webpack配置react+es6开 ...

  2. React Native开发入门

    目录: 一.前言 二.什么是React Native 三.开发环境搭建 四.预备知识 五.最简单的React Native小程序 六.总结 七.参考资料   一.前言 虽然只是简单的了解了一下Reac ...

  3. React阶段开发总结

    这次独立编写了React页面主要是数据切换.点击不同的按钮,Ajax请求不同的后台数据.数据驱动表格内容的显示.使用React组件开发. 开发中获得下面的心得: 1.后台给的地址早一点添加路由(写好数 ...

  4. React Native开发技术周报2

    (1).资讯 1.React Native 0.22_rc版本发布 添加了热自动重载功能 (2).技术文章 1.用 React Native 设计的第一个 iOS 应用 我们想为用户设计一款移动端的应 ...

  5. React Native开发技术周报1

    (一).资讯 1.React Native 0.21版本发布,最新版本功能特点,修复的Bug可以看一下已翻译 重要:如果升级 Android 项目到这个版本一定要读! 我们简化了 Android 应用 ...

  6. DECO 一个REACT NAtive 开发IDE工具

    DECO 一个REACT NAtive 开发IDE工具. 目前只支持 OS,NO WINDOWS https://www.decosoftware.com/ 一个方便的快速 ERXPRESS 教程:h ...

  7. React Native 开发之 (02) 用Sublime 3作为React Native的开发IDE

    Sublime Text是一个代码编辑器.也是HTML和散文先进的文本编辑器.漂亮的用户界面和非凡的功能,例如:迷你地图,多选择Python插件,代码段等等.完全可自定义键绑定,菜单和工具栏等等.漂亮 ...

  8. React Native 开发笔记

    ReactNativeDemo 学习ReactNative开发,搭建ReactNative第一个项目 React Native 开发笔记 1.安装Homebrew $ /usr/bin/ruby -e ...

  9. [转载]Sublime Text 3 搭建 React.js 开发环境

    [转载]Sublime Text 3 搭建 React.js 开发环境 Sublime有很强的自定义功能,插件库很庞大,针对新语言插件更新很快,配合使用可以快速搭建适配语言的开发环境. 1. babe ...

  10. Webpack+React配合开发

    前面两篇关于webpack的基础和进阶,请先务必阅读之前的文章. Webpack教程一 Webpack教程二 什么是React React是一个由Facebook开发的library,它的口号是“A ...

随机推荐

  1. CentOS7 修改Jenkins以root用户运行

    修改Jenkins以root用户运行,这在正式环境中是不可取的,但在自己的测试环境中就无所谓了啦,怎么方便怎么来. 1. 修改Jenkins配置文件1.1 修改$JENKINS_USER打开jenki ...

  2. IIS配置文件的XML格式不正确 applicationHost.config崩溃

    错误提示如图: 检查C:\Windows\System32\inetsrv\config目录下的applicationHost.config文件,备份一份. 可使用IIS提供的AppCmd.exe的r ...

  3. 管道模型(Pipeline)

    1.使用make_blobs来生成数据集,然后对数据集进行预处理 #导入数据集生成器 from sklearn.datasets import make_blobs #导入数据集拆分工具 from s ...

  4. 【CH1809】匹配统计(KMP)

    题目链接 摘自https://www.cnblogs.com/wyboooo/p/9829517.html 用KMP先求出以a[i]为结尾的前缀与b匹配的最长长度. 比如 f[i] = j,就表示a[ ...

  5. springboot activiti 工作流版本 集成代码生成器 shiro 安全框架

    官网:www.fhadmin.org 工作流模块---------------------------------------------------------------------------- ...

  6. ASPxClientGridView(客户端选择某一行的值的调用)

  7. HTTP缓存字段总结

    首部 通用首部:有些首部提供了与报文相关的最基本的信息,它们被称为通用首部. 请求首部:请求首部是只在请求报文中有意义的首部. 响应首部 实体首部: 用来描述HTTP报文的负荷,由于请求和响应报文中都 ...

  8. 解决 Orange Pi 烧录完系统后剩余可用空间过少的问题

    输入命令 df -ha 这图是拿别人的 看到系统才使用3.2g,内存卡有16g,不可能满的. 执行命令,加上sudo,防止权限不够: sudo fs_resize 如果上面那个不行的话,试试这个命令( ...

  9. 朴素贝叶斯算法源码分析及代码实战【python sklearn/spark ML】

    一.简介 贝叶斯定理是关于随机事件A和事件B的条件概率的一个定理.通常在事件A发生的前提下事件B发生的概率,与在事件B发生的前提下事件A发生的概率是不一致的.然而,这两者之间有确定的关系,贝叶斯定理就 ...

  10. viewer 图片点击放大 用法汇总

    A 不用viewer插件 1弹出框 https://www.cnblogs.com/web1/p/8989967.html 2表格中 https://www.jianshu.com/p/c17f4f6 ...