父组件向子组件传值

父组件通过属性进行传递,子组件通过props获取

//父组件
class CommentList extends Component{
render(){
return(
<div>
<Comment comment = {information} />
</div>
)
}
} //子组件
class Comment extends Component{
render(){
return(
<div>
<p>{this.props.comment}</p>
</div>
)
}
}

父组件CommentList 引用子组件Comment时设置comment属性传递information,

再组件comment中通过this.props.comment获取到information的值

子组件向父组件传值

通过回调进行传递数据

//父组件
export default class Home extends Component {
constructor(props){
super()
this.state={
data:"请选择你喜欢的课程"
}
} aa=(item)=>{
this.setState({
data:item
})
} render() {
console.log(this.state.data)
return (
<div>
<div>
<input type="text" placeholder={this.state.data} /></div>
<Item a={this.aa} />
</div>
)
}
}
//子组件
export default class Item extends Component {
constructor(props){
super()
this.state={
data:[],
state:false
}
}
render() {
// console.log(this.props)
return (
<div style={{display:this.state.state?"none":"block"}}>
{
this.state.data.map((val,index)=>{
return <div key={index} className="d1" onClick={()=>{
this.setState({
data:this.state.data,
state:!this.state.state
})
this.props.a(val.title)
}}>{val.title}</div>
})
}
</div>
)
}
// 发送请求
componentDidMount(){
axios.get("http://localhost:3000/data.json").then((response)=>{
console.log(response.data.data)
this.setState({
data:response.data.data
})
}).catch((error)=>{
console.log(error)
})
}
}
//json文件 data.json
{
"data": [
{
"id": "0",
"title": "Vue.js"
},
{
"id": "1",
"title": "AngularJS"
},
{
"id": "2",
"title": "JavaScript"
},
{
"id": "3",
"title": "React.js"
}
]
}
兄弟组件之间的传值
通过相同的父组件进行传递数据
子组件A通过回调函数形式将数据传给父组件,接着父组件通过属性将数据传递给子组件B

通过发布/订阅进行传递

在子组件A中 commentDidMount函数中,发布事件,在子组件B中commentDidMount函数中对事件进行监听

使用context进行传递

使用redux对state进行统一管理

react-组件间的传值的更多相关文章

  1. react组件间的传值方法

    关于react的几个网站: http://react.css88.com/ 小书:http://huziketang.mangojuice.top/books/react/ http://www.re ...

  2. React 组件间通讯

    React 组件间通讯 说 React 组件间通讯之前,我们先来讨论一下 React 组件究竟有多少种层级间的关系.假设我们开发的项目是一个纯 React 的项目,那我们项目应该有如下类似的关系: 父 ...

  3. React 组件间通信介绍

    React 组件间通信方式简介 React 组件间通信主要分为以下四种情况: 父组件向子组件通信 子组件向父组件通信 跨级组件之间通信 非嵌套组件间通信 下面对这四种情况分别进行介绍:   父组件向子 ...

  4. vue 和 react 组件间通信方法对比

    vue 和 react 组件间通信方法对比: 通信路径 vue的方法 react的方法 父组件 => 子组件 props(推荐).slot(推荐).this.$refs.this.$childr ...

  5. vue组件定义方式,vue父子组件间的传值

    vue组件定义方式,vue父子组件间的传值 <!DOCTYPE html> <html lang="zh-cn"> <head> <met ...

  6. Vue—非父子组件间的传值(Bus/发布订阅模式/观察者模式/总线)

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

  7. react组件间传值详解

    一.父子组件间传值     <1>父传子         父组件:

  8. React 组件间传值

    壹  .了解React传值的数据 一. 创建组件的方法 一 . 1  通过function声明的组件特点是: 1)function创建的组件是没有state属性,而state属性决定它是不是有生命周期 ...

  9. React组件间通信

    众所周知,ReactJS组件与组件之间的通信是一个难点.在React实际开发中,父子组件之间的传值是比较常见的,刚入门的小伙伴很容易被组件之间的通信绕懵. 今天花了点时间总结了一下React父子组件之 ...

  10. react - 组件间的值传递

    父组件向子组件传值 父组件通过属性进行传递,子组件通过props获取 //父组件 class CommentList extends Component{ render(){ return( < ...

随机推荐

  1. 【RN - 基础】之Text使用简介

    基本用法 Text组件是React Native中的一个重要组件,相当于iOS中的UILabel和Android中的TextView.Text组件用来存放文本数据.下面是一个简单的例子: import ...

  2. LNMP Shell脚本发布

    #!/bin/bash # : #This author is DKS #auto install nginx mysql php ################################## ...

  3. shell脚本编程基础--文本比较

    1.概述 允许测试Linux文件系统上文件的目录和状态. 2.详解 2.1 检查目录 -d测试会检查指定的目录是否存在于系统中.当我们打算将文件写入目录或是准备切换到该目录时,先进行测试是比较好的做法 ...

  4. 带着canvas去流浪系列之三 绘制饼图

    [摘要] 用canvas原生API绘制Echarts图表 示例代码托管在:http://www.github.com/dashnowords/blogs 一. 任务说明 使用原生canvasAPI绘制 ...

  5. Object类和@Data注解

    特别说明:若是有不对的地方欢迎指正 简要概述: Object类是java中所有类默认继承的一个类.下面介绍一下Object类中的一些重要的方法,面试中也是经常会被问到的.尤其是==和equals的区别 ...

  6. P2910 [USACO08OPEN]寻宝之路Clear And Present Danger |Floyd

    题目描述 农夫约翰正驾驶一条小艇在牛勒比海上航行. 海上有N(1≤N≤100)个岛屿,用1到N编号.约翰从1号小岛出发,最后到达N号小岛. 一张藏宝图上说,如果他的路程上经过的小岛依次出现了Ai,A2 ...

  7. ios中陀螺仪CoreMotion的用法

    转自:http://code.eoe.cn/471/title/ios涓檧铻轰华CoreMotion鐨勭敤娉 README.md 外部引用 原始文档 以前在iphone中要得到加速度时,只能使用Ac ...

  8. mac 安装 XX-Net-3.12.11

    目录 一 下载xx-net稳定版 二 使用Chrome浏览器 三 开启IPv6 mac禁用SIP机制 SIP 步骤 其他 附完整的csrutil命令 一 下载xx-net稳定版 github稳定版-X ...

  9. 强化学习三:Dynamic Programming

    1,Introduction 1.1 What is Dynamic Programming? Dynamic:某个问题是由序列化状态组成,状态step-by-step的改变,从而可以step-by- ...

  10. jqurey(尺寸,css操作,效果,遍历)

    尺寸: height():设置或返回元素的高度(不包括内边距.边框或外边距). width():设置或返回元素的宽度(不包括内边距.边框或外边距). 例如: $("#box").h ...