实例:

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

最新文章

  1. 生成的API分析文件太大。我们无法在交付前验证您的API使用信息。这只是通知信息。
  2. osg矩阵变换节点-----平移旋转缩放
  3. 创建如下三个类:(People类中的三个方法分别输出一些信息,ChinaPeople 和AmericanPeople类重写父类的三个方法)。
  4. Stopwatch 类
  5. ImageSwitcher自定意效果+定时切换图片
  6. 不连数据库List分页
  7. Verilog语法基础讲解之参数化设计
  8. Oracle 学习系列之一(表空间与表结构)
  9. JZ2440开发笔记(5)——通过按键点亮LED
  10. AS3.0面向对象的写法,类和实例
  11. highcharts-Highmaps 动态传入城市名称
  12. jsp定时方法
  13. Webdriver常用的元素定位
  14. Hanoi汉诺塔问题——递归与函数自调用算法
  15. hdu4746 Mophues
  16. 设计模式C++学习笔记之一(Strategy策略模式)
  17. windows 性能监视器
  18. ubuntu16.04 64bit 升级到 python3.6
  19. 联想笔记本thinkpad按F2不能直接重命名
  20. 【SQLSERVER】处理一对多标签的语法糖

热门文章

  1. java之hibernate之session中对象的生命周期
  2. Tomcat - 控制台乱码
  3. Laravel5.6---从头做一个项目web
  4. maven cmd 命令
  5. python(字符串函数)
  6. 【JUC】6.线程池—ThreadPoolExecutor
  7. RabbitMQ java 原生代码
  8. System V共享内存
  9. @ConfigurationProperties绑定配置信息至Array、List、Map、Bean
  10. abp学习(一)