基于react組件之間的參數(shù)傳遞(詳解)
更新時(shí)間:2017年09月05日 07:49:05 作者:壹然
下面小編就為大家?guī)硪黄趓eact組件之間的參數(shù)傳遞(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
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ù)傳遞(詳解)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用webpack配置react-hot-loader熱加載局部更新
這篇文章主要介紹了使用webpack配置react-hot-loader熱加載局部更新,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01react?umi?刷新或關(guān)閉瀏覽器時(shí)清除localStorage方式
這篇文章主要介紹了react?umi?刷新或關(guān)閉瀏覽器時(shí)清除localStorage方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10詳解create-react-app 2.0版本如何啟用裝飾器語法
這篇文章主要介紹了詳解create-react-app 2.0版本如何啟用裝飾器語法,cra2.0時(shí)代如何啟用裝飾器語法呢? 我們依舊采用的是react-app-rewired, 通過劫持webpack cofig對(duì)象, 達(dá)到修改的目的2018-10-10一文詳解React Redux設(shè)計(jì)思想與工作原理
最近看項(xiàng)目中使用了?Redux,?便嘗試了解一波?Redux?的設(shè)計(jì)思想與工作原理,所以本文詳細(xì)的給大家介紹了Redux設(shè)計(jì)思想與工作原理,需要的朋友可以參考下2023-09-09