React Native創(chuàng)建高階組件(修飾器)

創(chuàng)建

兩種方式:

代理方式的高階組件

function containerWrapper(Container, otherProps) {
  // 也可以使用無狀態(tài)組件
  class WrappedComponent extends Component {
    componentWillMount() {
      // Container的componentWillMount也會(huì)執(zhí)行
      console.log('wrapper')
    }
    
    render() {
      return (
        <View style={{flex: 1}}>
          <Container {...this.props} {...otherProps}/>
        </View>
      )
    }
  }
  // 或者
  // let WrappedComponent = (props) => (
  //   <View style={{flex: 1}}>
  //     <Container {...props} {...otherProps}/>
  //   </View>
  // )
  return connect(
    state => ({state}),
    dispatch => ({actions: bindActionCreators(actions, dispatch)})
  )(WrappedComponent);
}

繼承方式的高階組件

function containerWrapper(Container) {
  // 繼承Container
  class WrappedComponent extends Container {
    componentWillMount() {
      // 需要調(diào)用super
      super.componentWillMount()
      console.log('wrapper')
    }
    render() {
      return (
        <View style={{flex: 1}}>
          {super.render()}
        </View>
      )
    }
  }

  return connect(
    state => ({state}),
    dispatch => ({actions: bindActionCreators(actions, dispatch)})
  )(WrappedComponent);
}

調(diào)用

export default containerWrapper(TopicPage, {statusTextStyle: 'dark'})

兩種方式都可以包裝組件、擴(kuò)展生命周期方法。
不同點(diǎn)是前者實(shí)際創(chuàng)建了兩個(gè)組件,后者因?yàn)槭抢^承,所以只有一個(gè);前者可以增刪props,后者沒找到控制props的方法。

RNN通過組件的靜態(tài)屬性來設(shè)置navigator,使用代理方式的高階組件包裹后,訪問不到這些靜態(tài)屬性,需要在高階組件中加入兩行。如果是繼承方式就不需要

static navigatorStyle = Container.navigatorStyle
static navigatorButtons = Container.navigatorButtons

使用修飾器

引入修飾器(Decorator)簡化代碼,模仿connect修改高階組件

作為高階組件,都有必要轉(zhuǎn)換為修飾器實(shí)現(xiàn)

一、npm i --save-dev babel-plugin-transform-decorators-legacy

修改.babelrc

{
    "presets": ["react-native"],
    "plugins": ["transform-decorators-legacy"]
}

二、模仿redux connect修改container的高階組件。分解為兩層函數(shù),第一層的參數(shù)為需要的props,第二層的參數(shù)為組件

export const containerWrapper = (title) => {
  return (Container)=>{
    let WrappedComponent = (props)=> <Container {...props} title={title}/>
    ...
    return WrappedComponent
  }
}

三、調(diào)用

普通方式:

class TopicPage extends Component {...}
export default containerWrapper({title: '話題'})(TopicPage)

修飾器調(diào)用:

@containerWrapper({title: '話題'})
export default class TopicPage extends Component {...}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在目前的前端社區(qū),『推崇組合,不推薦繼承(prefer composition than inheritance)...
    Wenliang閱讀 78,065評論 16 124
  • 高階組件是對既有組件進(jìn)行包裝,以增強(qiáng)既有組件的功能。其核心實(shí)現(xiàn)是一個(gè)無狀態(tài)組件(函數(shù)),接收另一個(gè)組件作為參數(shù),然...
    柏丘君閱讀 3,217評論 0 6
  • In this article we will discuss how to use Higher Order C...
    人頭原子彈閱讀 692評論 0 0
  • 2015年2月18日正是農(nóng)歷年的除夕,晚上十點(diǎn),看著春晚卻突覺一陣寂寞,忍不住寫下一些什么。 我已經(jīng)很多天沒有在簡...
    環(huán)形山閱讀 337評論 0 2
  • 小米今天站在衣帽間,奢侈的花了30分鐘時(shí)間選了一套精神而喜氣的紅裙子,她不希望被人看出昨晚未睡的狼狽,還特地上...
    旺財(cái)楊子閱讀 721評論 1 50

友情鏈接更多精彩內(nèi)容