<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
        主站蜘蛛池模板: 亚欧色视频在线观看免费| 一区二区三区免费在线观看| 亚洲午夜成人精品电影在线观看| 老司机亚洲精品影视www| 久久精品国产亚洲AV无码娇色| 亚洲av永久无码精品天堂久久| 先锋影音资源片午夜在线观看视频免费播放| 一个人免费观看视频www| 亚洲精品午夜无码专区| 中文在线观看永久免费| 国产精品色午夜视频免费看| 亚洲AV电影天堂男人的天堂| 亚洲福利精品电影在线观看| 中文字幕在线免费播放| 亚洲av午夜成人片精品网站 | 麻豆一区二区免费播放网站| 亚洲日本国产精华液| 中文在线免费不卡视频| 久久91亚洲精品中文字幕| 24小时日本电影免费看| 亚洲综合国产一区二区三区| 日韩精品免费在线视频| 亚洲一区二区三区无码国产| 日韩毛片免费在线观看| 亚洲人成人77777网站不卡| 免费爱爱的视频太爽了| 国产黄在线播放免费观看| 免费国产怡红院在线观看| 亚洲成人免费在线观看| 日韩精品免费电影| 中文字幕免费观看视频| 亚洲精品午夜久久久伊人| 日韩精品在线免费观看| 亚洲小说图区综合在线| 亚洲国产精品综合久久网络| 一级特黄aa毛片免费观看| 亚洲老熟女五十路老熟女bbw| 久久笫一福利免费导航| 国产精品黄页免费高清在线观看| 久久99亚洲网美利坚合众国| jizzjizz亚洲|