国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

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

來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:04:07
文檔

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

React父子組件間的傳值的方法:父組件向子組件傳值: 父組件: import React, { Component } from 'react'; import Child from './chlid'; class parent extends Component{ constructor(props) { super(props); this.state = { tx
推薦度:
導(dǎo)讀React父子組件間的傳值的方法:父組件向子組件傳值: 父組件: import React, { Component } from 'react'; import Child from './chlid'; class parent extends Component{ constructor(props) { super(props); this.state = { tx

父組件向子組件傳值:

父組件:

import React, { Component } from 'react';
import Child from './chlid';

class parent extends Component{
 constructor(props) {
 super(props);
 this.state = {
 txt0:"默認(rèn)值0",
 txt1:"默認(rèn)值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

補(bǔ)充:

子組件向父組件傳值,

同樣是父組件:

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ù)里面進(jìn)行操作

 });

 

 }
 render() {

 return (

 <div>

 //通過綁定事件進(jìn)行值的運(yùn)算,這個(gè)地方一定要記得.bind(this),

 不然會(huì)報(bào)錯(cuò),切記切記,因?yàn)橥ㄟ^事件傳遞的時(shí)候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)//這個(gè)地方把值傳遞給了props的事件當(dāng)中

 }

 render() {

 return (

 <div className="list">

 <ul>

 {

 this.props.arr.map(item => {

 return (

 <li key={item.userName}>

 {item.userName} 評(píng)論是:{item.text}

 </li>

 )

 })

 }

 </ul>

 //通過事件進(jìn)行傳值,如果想得到event,可以在參數(shù)最后加一個(gè)event,

 這個(gè)地方還是要強(qiáng)調(diào),this,this,this

 <button onClick={this.clickFun.bind(this, this.state.childText)}>

 click me

 </button>

 </div>

 )

 }

}
export default ComentList; 

before:

after:

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

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

React父子組件間的傳值的方法:父組件向子組件傳值: 父組件: import React, { Component } from 'react'; import Child from './chlid'; class parent extends Component{ constructor(props) { super(props); this.state = { tx
推薦度:
標(biāo)簽: 方法 的方法 組件
  • 熱門焦點(diǎn)

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 国产91精品对白露脸全集观看 | 亚洲精品综合久久中文字幕 | 精品国产高清a毛片无毒不卡 | 91精品久久久 | 黄片毛片一级 | 激情欧美一区二区三区中文字幕 | 亚洲欧美在线视频 | 国产偷亚洲偷欧美偷精品 | 亚洲五月婷婷 | 亚洲欧美日韩综合在线播放 | 一边摸一边爽一边叫床视频 | 日韩不卡一区二区 | 欧美中文日韩 | 精品在线播放 | 一级黄免费 | 国产va免费精品观看 | 最新国产视频 | 欧美色图在线观看 | 国产成人精品免费午夜app | 亚洲色图 第一页 | 国产在线精选免费视频8x | 91精品久久久 | 欧美在线日韩 | 中文字幕另类 | 国产日韩一区二区三区在线观看 | 国产福利一区二区 | 337p日本欧洲亚洲大胆精品 | 久久久91精品国产一区二区 | 亚洲精品不卡久久久久久 | 亚洲一区二区免费 | 国产精品女同一区二区久久 | 伊人精品成人久久综合欧美 | 永久免费毛片 | 国产精品高清一区二区三区不卡 | 日韩欧美色视频 | 性夜影院爽黄a爽免费看网站 | 亚洲综合一区二区精品久久 | 欧美视频精品一区二区三区 | 国产亚洲视频在线观看 | 欧美综合自拍亚洲综合 | 日韩免费视频在线观看 |