react父子组件传参

父级向子级传参:在父组件中,我们引入子组件,通过给子组件添加属性,来起到传参的作用,子组件可以通过props获取父组件传过来的参数。

在父组件中:

import React from 'react'
import ChildCom from './childCom.js'
class ParentCom extends React.Component {
render() {
return (
<div>
<h1>父组件</h1>
<ChildCom content={'我是父级过来的内容'}/>
</div>
)
}
}
export default ParentCom;

在子组件中:

import React from 'react'
class ChildCom extends React.Component {
render() {
return (
<div>
<h2>子组件</h2>
<div>
{this.props.content}
</div>
</div>
)
}
}
export default ChildCom;

子级向父级传参:在父组件中给子组件添加一个属性,这个属性的内容为一个函数,然后在子组件中调用这个函数,即可达到传递参数的效果

在子组件中:

import React from 'react'
class ChildCom extends React.Component {
valueToParent(value) {
this.props.onValue(value);
}
render() {
return (
<div>
<h2>子组件</h2>
<div>
<a onClick={this.valueToParent.bind(this,)}>向父组件传值567</a>
</div>
</div>
)
}
}
export default ChildCom;

在父组件中:

import React from 'react'
import ChildCom from './childCom.js'
class ParentCom extends React.Component {
state = {
getChildValue: ''
}
getChildValue(value) {
this.setState({
getChildValue: value
})
}
render() {
return (
<div>
<h1>父组件</h1>
<div>子组件过来的值为:{this.state.getChildValue}</div>
<ChildCom onValue={this.getChildValue.bind(this)}/>
</div>
)
}
}
export default ParentCom;

最新文章

  1. js面向对象
  2. iOS RESideMenu 侧滑 第三方类库
  3. 关于Xcode8.1 / iOS10+ 真机测试系统打印或者宏定义打印不显示问题
  4. 基本I/O模型与Epoll简介
  5. JVM知识点
  6. Android EditText 文本框实现搜索和清空效果
  7. Python3基础 多分支结构 if-elif-else
  8. storm集成kafka
  9. 能够兼容ViewPager的ScrollView
  10. SPI协议总结
  11. 从Object和Function说说JS的原型链
  12. Kafka 安装配置 windows 下
  13. vue-cli环境配置
  14. http-cookie、session、Token
  15. PAT 1026 程序运行时间
  16. android 组件使用()
  17. 2018 ACM 网络选拔赛 北京赛区
  18. WordConuts
  19. 质控工具之TrimGalore使用方法
  20. IDEA Maven项目 编译的问题

热门文章

  1. 关于BFC的总结
  2. 查看ubuntu系统信息
  3. jmeter性能工具 使用手册(一)
  4. 对getBoundingClientRect属性的研究
  5. html5 figure和figcaption
  6. ZOJ 3329 One Person Game(概率DP,求期望)
  7. Linux C编程
  8. What is httpcontext
  9. MySQL分组聚合group_concat + substr_index
  10. 奖项-MVP:MVP(微软最有价值专家)百科