基于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)文章
Electron整合React使用搭建開發(fā)環(huán)境的步驟詳解
這篇文章主要介紹了Electron整合React使用搭建開發(fā)環(huán)境,本文分步驟給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值 ,需要的朋友可以參考下2020-06-06
react-native使用leanclound消息推送的方法
這篇文章主要介紹了react-native使用leanclound消息推送的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
深入React?18源碼useMemo?useCallback?memo用法及區(qū)別分析
這篇文章主要為大家介紹了React?18源碼深入分析useMemo?useCallback?memo用法及區(qū)別,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04

