基礎用法、父子通訊①傳值②接收
在myheader元件中接收通過mytitle屬性給 傳遞的資料
this.props.mytitle
var myheader =react.createclass(})reactdom.render(
, document.getelementbyid('example')
)
父與子通訊
在react中,可以通過自定義屬性傳乙個普通的字串,還可以傳乙個方法子與父通訊的標準版流程:
①父定義乙個有引數的方法
rcv:function(msg){}
②將此方法傳遞給子元件
③子元件來呼叫由引數方法,將資料傳遞到父元件
this.props.func(123)
var mybutton =react.createclass(,render:
function
()>
} })
var mycart =react.createclass(,
funcsubmit:
function
(), render:
function
()>
this.funcsubmit}>
} })
reactdom.render(
, document.getelementbyid('example')
)
兄弟通訊reactjs中 並沒有直接提供兄弟通訊的解決方案:
借助於共同的父元件來完成兄弟通訊過程
this.props.children
元件類this.props物件中的keyvalue,和呼叫元件時所指定的屬性是一一對應的;其實有乙個例外:this.props.children
可以通過this.props.children來獲取到元件在被呼叫時 內部的子元素
注意事項:
this.props.children型別是不確定的,如果乙個字標籤都沒有:undefined
如果只有乙個:object
如果有多個:array
react中實現乙個方法來方便的遍歷this.props.children:
react.children.map(
this.props.children,
(value)=>
)
React元件間資訊傳遞方式
父元件定義乙個 name get constructor props render 子元件通過 this.props 接收 render this.props return 先在父元件定義乙個方法 constructor props this.fn this.fn.bind this 如果裡面有值就...
react路由引數傳遞
react路由的三種傳參方式 1 向路由元件傳遞params引數 引數傳遞 在註冊路由時接收引數 注意這裡後面時冒號在前面的 在要展示的元件內接收params引數 2 第二種,利用search傳遞引數 向路由元件傳遞引數 這種方式類似js常用方式傳遞引數 在路由註冊是接收引數 這裡是不需要接收的,正...
函式間傳遞引數
function example num example 10 自定義函式簡單示例 結果為 10 10 100 在函式間傳遞引數 1,按值傳遞方式 function example m m 1 example m echo 在函式外 m m 結果為 在函式內 a 15 在函式外 a 1 2,按引用傳...