React父子組件間的傳值的方法
父組件向子組件傳值:
父組件:
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:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
聊聊ant?design?charts?獲取后端接口數(shù)據(jù)展示問題
今天在做項(xiàng)目的時(shí)候遇到幾個(gè)讓我很頭疼的問題,一個(gè)是通過后端接口成功訪問并又返回?cái)?shù)據(jù),但拿不到數(shù)據(jù)值。其二是直接修改state中的data,console中數(shù)組發(fā)生變化但任然數(shù)據(jù)未顯示,這篇文章主要介紹了ant?design?charts?獲取后端接口數(shù)據(jù)展示,需要的朋友可以參考下2022-05-05
Remix?路由模塊輸出對(duì)象handle函數(shù)
這篇文章主要為大家介紹了Remix?路由模塊輸出對(duì)象handle函數(shù)使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
React?中hooks之?React.memo?和?useMemo用法示例總結(jié)
React.memo是一個(gè)高階組件,用于優(yōu)化函數(shù)組件的性能,通過記憶組件渲染結(jié)果來避免不必要的重新渲染,合理使用React.memo和useMemo可以顯著提升React應(yīng)用的性能,本文介紹React?中hooks之?React.memo?和?useMemo用法總結(jié),感興趣的朋友一起看看吧2025-01-01
react 路由跳轉(zhuǎn)的7種方式實(shí)現(xiàn)
本文介紹了React中六種常見的路由跳轉(zhuǎn)方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03
react native 原生模塊橋接的簡(jiǎn)單說明小結(jié)
這篇文章主要介紹了react native 原生模塊橋接的簡(jiǎn)單說明小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
React使用hook如何實(shí)現(xiàn)數(shù)據(jù)雙向綁定
這篇文章主要介紹了React使用hook如何實(shí)現(xiàn)數(shù)據(jù)雙向綁定方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03

