补充知识点
1==》npm install prop-types 先安装参数校验包 在B C页面引入
import PropTypes from 'prop-types' //参数限制
    // 验证 参数必要性  B页面
static propTypes = {
len: PropTypes.number.isRequired,
addtod: PropTypes.func.isRequired
}
// 验证 参数必要性C页面
static propTypes = { todolist: PropTypes.array.isRequired }

关于todelist的总结

1==》将数据放在父组件中

    constructor(props){
super(props);
this.state={
todolist:[
{ id: 1, text: "web111" },
{ id: 2, text: "web222" },
{ id: 3, text: "web333" }
]
}
} 2==》将父组件中的数据 传递给子组件(父传子)
let { todolist}=this.state; //结构
<C todolist={todolist}></C> 3==》子组件进行渲染
render() {
let { todolist } = this.props;
console.log("值传递过来",todolist)
return (
<ul>
{todolist.map((item,index)=>{
return <li key={index}>{item.text}</li>
})}
</ul>
)
} 4==》将父组件的长度 传递给子组件
<B len={todolist.length} addtod={this.addtod}></B> 父 5==》 子组件进行渲染
render() {
let { len}=this.props
return (
<div>
<input type="text" ref="conn" /><button onClick={this.add} >123#{len}</button>
</div>
)
} 6==》点击按钮获取到值 render() {
let { len}=this.props
return (
<div>
<input type="text" ref="conn" /><button onClick={this.add} >123#{len}</button>
</div>
)
} add=()=>{
console.log(this.refs.conn.value) let uservalu = this.refs.conn.value; //获取值 let {addtod}=this.props;//父组件想子组件传递了一个方法 addtod(uservalu) //调用这个方法 this.refs.conn.value=""; //清空
} 7==》父组件给子组件传递方法 8==》子组件调用父组件的方法 并且返回表单中的内容 9==》父组件接受子组件中返回来的数据 更改state

以下是完整代码

A.js  标题

import React, { Component } from "react"
export default class A extends Component {
render() {
return (
<div>我是标题 todo list</div>
)
}
}

B.js 表单和按钮

import React, { Component } from "react"
export default class B extends Component { add=()=>{
console.log(this.refs.conn.value)
let uservalu = this.refs.conn.value; //获取值 let {addtod}=this.props;//父组件想子组件传递了一个方法 addtod(uservalu) //调用这个方法 this.refs.conn.value=""; //清空
} render() {
let { len}=this.props
return (
<div>
<input type="text" ref="conn" /><button onClick={this.add} >123#{len}</button>
</div>
)
}
}

C.js 渲染

import React, { Component } from "react"
export default class C extends Component {
// constructor(props){
// super(props);
// let { todolist}=this.props;
// } render() {
let { todolist } = this.props; //它等价于上面的哪一个内容
console.log("值传递过来",todolist)
return (
<ul>
{todolist.map((item,index)=>{
return <li key={index}>{item.text}</li>
})}
</ul>
)
}
}

最大父组件DoAddList.js

import React, { Component } from "react"

// 引入组件
import A from "./A"
import B from "./B"
import C from "./C" export default class DoAddList extends Component { constructor(props){
super(props);
this.state={
todolist:[
{ id: 1, text: "web111" },
{ id: 2, text: "web222" },
{ id: 3, text: "web333" }
]
}
} addtod=(data)=>{
let con=this.state.todolist;
con.unshift({ id: this.state.todolist.length + 1, text: data }) // 跟新状态
this.setState({
todolist: con
})
} render() {
let { todolist}=this.state; //结构
return (
<div>
<A></A>
{/* 将右边的{this.addtod 方法传递给子组件 */}
<B len={todolist.length} addtod={this.addtod}></B>
{/*将父组件中的数据 传递给子组件(父传子)*/}
<C todolist={todolist}></C>
</div>
)
}
}

  

React ToDolist增加功能的更多相关文章

  1. Android程序ToDoList增加配置项页面

    本文要做的事情就是在前面做的简单的ToDoList程序上增加一个配置项页面(Reference).这个Reference页面也非常简单: 这个ToDoList现在有两个页面,主页面能填写待办事项,然后 ...

  2. Cordova webapp实战开发:(7)如何通过简单的方法做到,不重新发布APP来修复bug、增加功能、或者躲开苹果的一些严格审核?

    到<Cordova webapp实战开发:(6)如何写一个iOS下获取APP版本号的插件?>为止,我们已经大体学会了如何使用Cordova了,那些都是使用Cordova的开发者必备的技能. ...

  3. React 环境增加Redux ,React-Redux

    引入 Redux 的目的, 状态管理! React-Redux 就是完成一些粘合剂的作用. 简而化之的理解就是将数据放在store 中维护, 操作逻辑放在reducer中去写. 更功利的表达就是:  ...

  4. todolist增加markdown模块

    markdown编辑器 利用`markdown_js`开源库实现todolist小项目的markdown日记本功能 todolist小项目地址 之前的介绍随笔todoList markdown-js仓 ...

  5. react todolist

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

  6. react native 增加react-native-camera

    前提:已经正常运行的项目 第一步:使用命令加入react-native-camera,并且关联react-native-camera, yarn add react-native-camera rea ...

  7. react todolist代码优化

    Todolist.js import React, { Component,Fragment } from 'react'; import TodoItem from './TodoItem'; im ...

  8. 修改struts2自定义标签的源代码,在原有基础上增加功能(用于OA项目权限判断,是否显示某个权限)

    OA项目在做权限判断时  原始方式: 现在完成的功能 :通过改变struts2自定标签源代码   在原有的基础上  增加判断权限的功能  而页面上使用标签的方式 还是下图 步骤: 打开文件 搜索< ...

  9. Vue学习之todolist删除功能

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

随机推荐

  1. lua 12 table 的使用

    转自:http://www.runoob.com/lua/lua-tables.html table 是 Lua 的一种数据结构用来帮助我们创建不同的数据类型,如:数组.字典等. Lua table ...

  2. ICCV

    ICCV 简介 ICCV 的全称是 IEEE International Conference on Computer Vision,即国际计算机视觉大会,由IEEE主办,与计算机视觉模式识别会议(C ...

  3. 工具资源系列之给 windows 虚拟机装个 centos

    前面我们已经介绍了如何在 Windows 宿主机安装 VMware 虚拟机,这节我们将利用安装好的 VMware 软件安装 centos 系统. 前情回顾 由于大多数人使用的 Windows 电脑而工 ...

  4. C语言前置知识汇编基础--总线【地址总线,控制总线,数据总线】练习题

    寻址就是用一个数字表示一个字节存储单元的地址,而不是表示这个存储单元中的内容,所以不用乘以8. 1. 1个CPU的寻址能力为8KB,那么它的地址总线宽度是多少? 8*1024=8192     共有8 ...

  5. 记录战斗记录你,详解妖尾战斗录像系统[Unity]

    .katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...

  6. 在Azure DevOps Server中运行基于Spring Boot和Consul的微服务项目单元测试

    1 概述 谈到微服务架构体系,绕不开服务发现这个功能.服务发现机制是简化微服务配置.实现容灾.水平扩缩容.提高运维效率的重要方式.在服务发现工具中,Consul在部署和使用方面与容器结合的天衣无缝,成 ...

  7. Uboot启动流程分析(一)

    1.前言 Linux系统的启动需要一个bootloader程序,该bootloader程序会先初始化DDR等外设,然后将Linux内核从flash中拷贝到DDR中,最后启动Linux内核,uboot的 ...

  8. redis之HyperLogLog

    HyperLogLog 提供不精确的去重计数方案,虽然不精确但是也不是非常不精确,标准误差是 0.81%. 使用方法 HyperLogLog 提供了两个指令 pfadd 和 pfcount,根据字面意 ...

  9. LeetCode 1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold

    题目 我是按照边进行二分的 class Solution { public: int sum[100005]; int a[305][305]; int maxSideLength(vector< ...

  10. shell脚本中判断进程是否存在

    #! /bin/bash PROC_NAME=docker ProcNumber=`ps -ef |grep -w $PROC_NAME|grep -v grep|wc -l` ];then echo ...