<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答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
        當(dāng)前位置: 首頁 - 科技 - 知識(shí)百科 - 正文

        react 組件傳值的三種方法

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

        react 組件傳值的三種方法

        react 組件傳值的三種方法:整理 react 組件傳值 三種方式 父組件向子組件傳值(通過props傳值) 子組件: class Children extends Component{ constructor(props){ super(props); } render(){ return( <div>這是:{this.props.name}
        推薦度:
        導(dǎo)讀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 當(dāng)做形參
         </div>
         }
         }
        

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

        子組件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)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。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}
        推薦度:
        標(biāo)簽: 方法 方式 三種方式
        • 熱門焦點(diǎn)

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 久久亚洲精品无码AV红樱桃| 国产真实伦在线视频免费观看| 波霸在线精品视频免费观看| 免费无码婬片aaa直播表情| 在线免费观看伊人三级电影| AV无码免费永久在线观看| 免费观看美女裸体网站| 国产乱辈通伦影片在线播放亚洲 | 美女视频黄a视频全免费网站色窝 美女被cao网站免费看在线看 | 一本久到久久亚洲综合| 亚洲国产精品无码一线岛国| 亚洲欧洲日产专区| 国产亚洲精品美女2020久久| 精品四虎免费观看国产高清午夜| 亚洲精品乱码久久久久66| 精品成人一区二区三区免费视频| 四虎永久免费影院| 亚洲av片不卡无码久久| 中文字幕在线免费看| 亚洲av成人无码久久精品| 无码免费又爽又高潮喷水的视频| 曰批视频免费30分钟成人| 亚洲男同帅GAY片在线观看| 理论秋霞在线看免费| 国产三级在线观看免费| 久久精品蜜芽亚洲国产AV| 免费看污成人午夜网站| 免费播放美女一级毛片| 精品亚洲综合久久中文字幕| 麻豆视频免费观看| 亚洲小视频在线观看| 两个人看的www高清免费观看| 亚洲精品亚洲人成在线麻豆| 8090在线观看免费观看| 亚洲精品无码MV在线观看 | 国产精品亚洲专区无码唯爱网| 日日麻批免费40分钟无码 | 免费a级毛片高清视频不卡| 老司机午夜在线视频免费观| 亚洲AV无码国产精品色午友在线| 丁香花在线观看免费观看图片|