hnrqss
hnrqss

Reputation: 41

How can i get values from react native into my bridge swift class?

Following docs i try to do something like this: Everything work's but the value myprop don't get into my ComponentView

Native Code

//component.h

#import "React/RCTBridgeModule.h"
#import "React/RCTViewManager.h"
#import "React/RCTUIManager.h"

@interface component: RCTViewManager

@property (nonatomic, assign) NSString *myProp;

@end


//component.m

#import "component.h"

#import <Foundation/Foundation.h>

@interface RCT_EXTERN_MODULE(ComponentViewManager, RCTViewManager)

RCT_EXPORT_VIEW_PROPERTY(myProp, NSString)

@end

//ComponentViewManager.swift

import Foundation

@objc(ComponentViewManager)
class ComponentViewManager: RCTViewManager {

    override func view() -> UIView! {
        return ComponentView()
    }

    ....
}

//ComponentView
class ComponentView: UIView {

    //initWithFrame to init view from code
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.frame = frame
        setupView()
    }

    func setupView() {
        ...
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    public func setMyProp(_ myProp: NSString) {
        //DON'T GET Here :(
        self.myProp = myProp
    }
}

What i do wrong myProp value don't get in my view? RN docs: https://facebook.github.io/react-native/docs/native-components-ios

Upvotes: 3

Views: 586

Answers (1)

hnrqss
hnrqss

Reputation: 41

Answering my own question :(, but i figured out how to solve and if could helps anyone.

you just to set one attribute with the same name of your prop that was registered into your .m and in your .h files.

//ComponentView

@objc var myProp = "" {
  didSet {
    //Here you can call any function and it will be called after the value get here
    doSomething()

  }
}

Upvotes: 1

Related Questions