基于react組件之間的參數(shù)傳遞(詳解)
更新時間:2017年09月05日 07:49:05 作者:壹然
下面小編就為大家?guī)硪黄趓eact組件之間的參數(shù)傳遞(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
1、父組件向子組件傳遞參數(shù)
class Child extends Component {
componentDidMount(){
let name = this.props.default;
console,log(name);
}
render(){
const { default} = this.props;
return (
<Input />
)
}
}
import React, { Component } from 'react';
import Child from './Child';
class Parent extends Component {
state = {
name: 'Bob'
}
render() {
return (
<div>
<Child default={this.state.name} />
</div>
)
}
}
2、子組件向父組件傳遞參數(shù)
class Child extends Component {
state={
name:'Bob'
}
componentDidMount(){
this.props.toParent(this.state.name);
}
render(){
return (
<Input />
)
}
}
import React, { Component } from 'react';
import Child from './Child';
class Parent extends Component {
state = {
name:''
}
getChildInfo = (name)=>{
this.setState({name:name});
}
render() {
return (
<div>
<Child toParent={this.getChildInfo.bind(this)} />
</div>
)
}
}
以上這篇基于react組件之間的參數(shù)傳遞(詳解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用webpack配置react-hot-loader熱加載局部更新
這篇文章主要介紹了使用webpack配置react-hot-loader熱加載局部更新,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
react?umi?刷新或關(guān)閉瀏覽器時清除localStorage方式
這篇文章主要介紹了react?umi?刷新或關(guān)閉瀏覽器時清除localStorage方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
詳解create-react-app 2.0版本如何啟用裝飾器語法
這篇文章主要介紹了詳解create-react-app 2.0版本如何啟用裝飾器語法,cra2.0時代如何啟用裝飾器語法呢? 我們依舊采用的是react-app-rewired, 通過劫持webpack cofig對象, 達(dá)到修改的目的2018-10-10

