国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

react 組件傳值的三種方法

來源:懂視網(wǎng) 責編:小采 時間:2020-11-27 21:55:47
文檔

react 組件傳值的三種方法

react 組件傳值的三種方法:整理 react 組件傳值 三種方式 父組件向子組件傳值(通過props傳值) 子組件: class Children extends Component{ constructor(props){ super(props); } render(){ return( <div>這是:{this.props.name}
推薦度:
導讀react 組件傳值的三種方法:整理 react 組件傳值 三種方式 父組件向子組件傳值(通過props傳值) 子組件: class Children extends Component{ constructor(props){ super(props); } render(){ return( <div>這是:{this.props.name}

整理 react 組件傳值 三種方式

父組件向子組件傳值(通過props傳值)

子組件:

 class Children extends Component{
 constructor(props){
 super(props);
 }
 render(){
 return(
 <div>這是:{this.props.name}</div> // 這是 父向子
 )
 }
 }

父組件:

 class App extends React.Component{
 render(){
 return(
 <div>
 <Children name="父向子"/>
 </div>
 )
 }
 }

父組件向子組件傳值(回調(diào)函數(shù))

子組件

 class Children extends Component{
 constructor(props){
 super(props);
 }
 handerClick(){
 this.props.changeColor('skyblue') // 執(zhí)行父組件的changeColor 并傳參 必須和父組件中的函數(shù)一模一樣
 }
 render(){
 return(
 <div>
 <div>父組件的背景色{this.props.bgcolor}</div> // 子組件接收父組件傳過來的值 bgcolor
 <button onClick={(e)=>{this.handerClick(e)}}>改變父組件背景</button> // 子組件執(zhí)行函數(shù)
 </div>
 )
 }
 }

父組件

 class Father extends Component{
 constructor(props){
 super(props)
 this.state = {
 bgcolor:'pink'
 }
 }
 bgChange(color){
 this.setState({
 bgcolor:color
 })
 }
 render(props){
 <div style={{background:this.state.bgcolor}}>
 // 給子組件傳遞的值 color 
 <Children bgcolor={this.state.bgcolor} changeColor={(color)=>{this.bgChange(color)}} /> 
 // changeColor 子組件的參數(shù)=color 當做形參
 </div>
 }
 }

兄弟組件傳值(子傳給父,父再傳給另一個子)

子組件1

 class Children1 extends Component{
 constructor(props){
 super(props);
 }
 handerClick(){
 this.props.changeChild2Color('skyblue') 
 // 改變兄弟組件的顏色 把changeChild2Color的參數(shù)傳給父
 }
 render(){
 return(
 <div>
 <div>children1</div>
 <button onClick={(e)=>{this.handerClick(e)}}>改變children2背景</button>
 </div>
 )
 }
 }

子組件2

 class Children2 extends Component{
 constructor(props){
 super(props);
 }
 render(){
 return(
 <div style={{background:this.props.bgcolor}}>
 // 從父元素獲取自己的背景色
 <div>children2 背景色 {this.props.bgcolor}</div>
 // children2 背景色 skyblue
 </div>
 )
 }
 } 

父組件

class Father extends Component{
 constructor(props){
 super(props)
 this.state = {
 child2bgcolor:'pink'
 }
 }
 onchild2bgChange(color){
 this.setState({
 child2bgcolor:color
 })
 }
 render(props){
 <div>
 <Children1 changeChild2Color={(color)=>{this.onchild2bgChange(color)}} />
 <Children2 bgcolor={this.state.child2bgcolor} />
 </div>
 }
}

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

react 組件傳值的三種方法

react 組件傳值的三種方法:整理 react 組件傳值 三種方式 父組件向子組件傳值(通過props傳值) 子組件: class Children extends Component{ constructor(props){ super(props); } render(){ return( <div>這是:{this.props.name}
推薦度:
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 亚洲国产成人久久综合碰碰动漫3d | 国产第4页 | 欧美另类网 | 国产毛片久久久久久国产毛片 | 天天做夜夜做久久做狠狠 | 久久精品视频一区二区三区 | 中文字幕va一区二区三区 | 91精品啪在线观看国产线免费 | 国产亚洲精品成人婷婷久久小说 | 日韩综合在线 | 激情欧美一区二区三区 | 欧美视频精品在线 | 久久国产精品视频 | 欧美国产精品va在线观看 | 国产欧美日韩综合 | 国产欧美日韩综合精品一区二区 | 亚洲天码中文字幕第一页 | 国产成人精品久久二区二区 | 国产日韩欧美亚洲综合首页 | 亚洲欧美成人综合久久久 | 国产亚洲视频在线 | 在线观看国产一区二区三区 | 国产亚洲综合成人91精品 | 日韩亚洲欧美一区二区三区 | 欧美一区二区三区在线 | 欧美 日韩 国产 成人 在线观看 | 免费观看国产精品 | 四虎影院永久免费 | 草逼电影| 欧美国产高清欧美 | 国产综合一区二区 | 欧美日韩一区二区在线 | 成人a毛片免费视频观看 | 国产精品视频福利一区二区 | 精品久久亚洲一级α | 欧美在线综合视频 | 亚洲色吧 | 久久99精品一区二区三区 | 国产精品久久久天天影视香蕉 | 欧美激情伦妇在线观看 | 视频一区 中文字幕 |