国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

React父子組件間的傳值的方法

2019-11-19 12:31:42
字體:
來源:轉載
供稿:網(wǎng)友

父組件向子組件傳值:

父組件:

import React, { Component } from 'react';import Child from './chlid';class parent extends Component{ constructor(props) {  super(props);  this.state = {   txt0:"默認值0",   txt1:"默認值1"  } } componentDidMount(){ } parToson(){  this.setState({   txt0:"哈哈哈哈"  }) } sonToPar(e){  this.setState({   txt1:e  }) } render(){  const style={   paddingLeft:"150px"  }  return(   <div style={style}>    <button onClick={this.parToson.bind(this)}>傳值給子組件</button>    <div>接受子組件的傳值為:{this.state.txt1}</div>    <br/>    <Child message={this.state.txt0} getsonToPar={this.sonToPar.bind(this)}/>   </div>  ) }}

子組件:

import React, { Component } from 'react';class child extends Component{ constructor(props) {  super(props);  this.state = {   msg:"啦啦啦啦"  } } componentDidMount(){ } render(){  return(   <div>    <div>接受父組件傳的值為:{this.props.message}</div>    <button onClick={()=>this.props.getsonToPar(this.state.msg)}>傳值給父組件</button>   </div>  ) }}export default child;

github:https://github.com/Rossy11/react/blob/master/src/component/router4.js

補充:

子組件向父組件傳值,

同樣是父組件:

import React from "react"import ComentList from "./ComentList"class Comment extends React.Component { constructor(props) {  super(props);  this.state = {   parentText: "this is parent text",   arr: [{    "userName": "fangMing", "text": "123333", "result": true   }, {    "userName": "zhangSan", "text": "345555", "result": false   }, {    "userName": "liSi", "text": "567777", "result": true   }, {    "userName": "wangWu", "text": "789999", "result": true   },]  } } fn(data) {  this.setState({   parentText: data //把父組件中的parentText替換為子組件傳遞的值  },() =>{   console.log(this.state.parentText);//setState是異步操作,但是我們可以在它的回調(diào)函數(shù)里面進行操作  });  } render() {  return (   <div>    //通過綁定事件進行值的運算,這個地方一定要記得.bind(this),   不然會報錯,切記切記,因為通過事件傳遞的時候this的指向已經(jīng)改變    <ComentList arr={this.state.arr} pfn={this.fn.bind(this)}>     </ComentList>    <p>     text is {this.state.parentText}    </p>     </div>   ) }}export default Comment;     

子組件:

import React from "react"class ComentList extends React.Component { constructor(props) {  super(props);  this.state = ({   childText: "this is child text"  }) } clickFun(text) {  this.props.pfn(text)//這個地方把值傳遞給了props的事件當中 } render() {  return (   <div className="list">    <ul>     {      this.props.arr.map(item => {       return (        <li key={item.userName}>         {item.userName} 評論是:{item.text}        </li>       )      })     }    </ul>    //通過事件進行傳值,如果想得到event,可以在參數(shù)最后加一個event,    這個地方還是要強調(diào),this,this,this    <button onClick={this.clickFun.bind(this, this.state.childText)}>     click me    </button>   </div>  ) }}export default ComentList;  

before:

after:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 高碑店市| 永康市| 旌德县| 凤山市| 佛教| 荔浦县| 那坡县| 广灵县| 芦山县| 崇文区| 昌江| 静安区| 武义县| 上虞市| 鸡西市| 望奎县| 雅安市| 香河县| 曲水县| 长岛县| 朝阳区| 建阳市| 阿坝| 义乌市| 明光市| 大新县| 图片| 绥棱县| 潜山县| 凉城县| 兴化市| 梁平县| 兴业县| 蕉岭县| 广安市| 南雄市| 阿城市| 罗田县| 平遥县| 永新县| 五河县|