Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native UI Component Method

I am in the process of creating my own custom ui component for react native in objective-c and so far everything works, except I can't seem to call any methods on my component. This is my objective-c

#import "RNTWebViewManager.h"
#import <WebKit/WebKit.h>

@interface RNTWebViewManager()

@property (strong, nonatomic) WKWebView *webView;

@end

@implementation RNTWebViewManager

@synthesize webView;

RCT_EXPORT_MODULE()

- (UIView *)view {
  webView = [[WKWebView alloc] init];
  return  webView;
}

RCT_EXPORT_METHOD(loadWebsite:(NSString *)url)
{
  RCTLogInfo(@"Opening url %@",url);
  [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
};

@end

And here is my corresponding javascript code:

import React, { Component } from 'react';
import {
  requireNativeComponent,
  AppRegistry,
  StyleSheet,
  Button,
  Text,
  View
} from 'react-native';

// Load Native iOS Components
var RNTWebView = requireNativeComponent('RNTWebView', null);

class WebView extends Component {
  render() {
    return (
      <RNTWebView style={styles.webView} />
    )
  }
}

export default class iOSComponents extends Component {
  componentDidMount() {
    RNTWebView.loadWebsite("http://www.google.com/");
  }
  render() {
    return (
      <View style={styles.webView}>
        <View style={styles.statusBar} />
        <WebView />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  statusBar: {
    height:22,
  },
  webView: {
    flex:1,
  }
});

AppRegistry.registerComponent('iOSComponents', () => iOSComponents);

When I try calling RNTWebView.loadWebsite("http://www.google.com") I get an error saying RNTWebView.loadWebsite is not a function.

like image 770
Asleepace Avatar asked Apr 21 '26 08:04

Asleepace


1 Answers

That's an incorrect way to call native component. You call it through the Native modules library.

import { NativeModules } from 'react-native';

// ..rest of your code

// The name of your native module is based on the objective c class name. I assume it to be 'RNTWebViewManager'
let RNTWebView = NativeModules.RNTWebViewManager

// call method
RNTWebView.loadWebsite('https://www.google.com');

For more info, the official documentation is here.

like image 165
Mμ. Avatar answered Apr 22 '26 23:04

Mμ.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!