前端学习(2233):react的子传父数据传递
生活随笔
收集整理的這篇文章主要介紹了
前端学习(2233):react的子传父数据传递
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import React from 'react';
import ReactDOM from 'react-dom';import './App.css'
class ParentCom extends React.Component {constructor(props) {super(props)this.state = {childData: null}}render() {return ( <div ><h1 > 子元素傳遞給父級 { this.state.childData } < /h1> <ChildCom setChildData = { this.setChildData }/ > < /div >)}setChildData = (data) => {this.setState({childData: data})}}class ChildCom extends React.Component {constructor(props) {super(props)this.state = {msg: "geyao"}}render() {let sendData = nullreturn ( <div ><button onClick = { this.sendData } > 傳遞helloworld給父元素 < /button> <button onClick = {() => { this.props.setChildData('直接調用') } } > 傳遞helloworld給父元素 < /button> </div >)}sendData = () => {console.log(this.state.msg)console.log(this.props.setChildData(this.state.msg))}}//console.log("這是渲染函數")
ReactDOM.render( < ParentCom / > , document.querySelector("#root"))
運行結果
總結
以上是生活随笔為你收集整理的前端学习(2233):react的子传父数据传递的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 前端学习(2156):uglifyjsw
- 下一篇: 《Head First 设计模式》例子的