2017-02-27 126 views
0

我是swift和react-native的新手。什麼是正確的方法來重新映射本機組件的屬性

有一個第三方本機組件,稱爲NativeComp,它是UIView的子類。

,我想將其包裝成反應天然組分和重新映射JS屬性天然屬性(一些重命名工作)

例如

JS

<RNComponent config={enable:true, size:5}/> 

會導致本機代碼。

component.enable=true 
component.size=5 
  • 方法1

    RNComponent延伸NativeComp,並添加一個新的調用setConfig,configue自這個功能。

    它有效,但有點不可思議。

  • 方法2 編寫一個獨立的UIView類ContainerView,將NativeComp添加爲子視圖。

    override init(frame: CoreGraphics.CGRect) { 
    
        self._component = NativeComp(frame: frame); 
    
        super.init(frame: frame); 
    
        self.addSubview(_component); 
    
    } 
    
    override func reactSetFrame(_ frame: CGRect) 
    { 
        _component.reactSetFrame(frame); 
    } 
    

    它看起來比方法1更清潔,但是因爲_component超出了ContainerView邊框,觸摸事件丟失了。

    ,如果我在self.reactSetFrame(frame);

    把它寫成

    override func reactSetFrame(_ frame: CGRect) 
    { 
        self.reactSetFrame(frame); 
        _component.reactSetFrame(frame); 
    } 
    

程序崩潰exec bad access我不明白爲什麼。

將js屬性重新映射到現有本地組件的正確方法是什麼?

回答

0

我的錯誤。

self.reactSetFrame(frame)會導致無限遞歸。

更改爲super.reactSetFrame後,一切正常。

相關問題