1.父組件向子組件傳遞數(shù)據(jù)
//父組件.js
let fatherData = [1,2,3,4]
<childComponent toChildData={ fatherData }>
//子組件childComponent.js 取到數(shù)據(jù)
data = this.props.toChildData
2.className 的三目運(yùn)算
<div className = { nowIndex === i ? 'active' : '' }
3.子組件向父組件通信需要使用父組件傳遞給子組件的事件
//子組件 childComponent.js
toEmitFather( paramsFromChild ){
this.props.fatherPickChildEvt ( paramsFromChild ) //調(diào)用父組件通過屬性傳遞過來的fatherPickChildEvt 事件
}
<li onClick = {this.toEmitFather.bind(this, paramsFromChild )}><li/> //如果需要傳遞參數(shù)給父組件用bind()
//父組件.js
childClick = ( paramsFromChild ) =>{ //!注意 這里一定要使用箭頭函數(shù), 不然this指向的不是父組件的實(shí)例而是childComponent實(shí)例
this.setState({})
}
<childComponent fatherPickChildEvt = {this.childClick}>