Skip to content

Instantly share code, notes, and snippets.

@panvourtsis
Last active January 3, 2019 13:04
Show Gist options
  • Save panvourtsis/714155aa2325b7b3b35bdb91f53fe2d3 to your computer and use it in GitHub Desktop.
Save panvourtsis/714155aa2325b7b3b35bdb91f53fe2d3 to your computer and use it in GitHub Desktop.
JSMeetup Native Module
package com.JSMeetup;
import ...
class JSMeetupModule extends ReactContextBaseJavaModule {
private static String mPremium = "";
public JSMeetupModule(ReactApplicationContext reactContext) {
super(reactContext);
...
}
@Override
public String getName() {
return "JSMeetup";
}
@ReactMethod
public void setPremium(String premium) {
mPremium = premium;
}
}
public class JSMeetupPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new JSMeetupModule(reactContext));
return modules;
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
List<ViewManager> modules = new ArrayList<>();
modules.add(new JSMeetupViewManager(reactContext));
return modules; // if we have a view else return Collections.emptyList();
}
}
class JSMeetupReactView extends Component {
setInputUrl(url) {
NativeModules.JSMeetup.setInputUrl(url);
}
blah() {
UIManager.dispatchViewManagerCommand(
findNodeHandle(this.refs.JSMeetupReactView),
UIManager.JSMeetupView.Commands.blah,
null
);
}
render() {
return <JSMeetupView
{...this.props}
ref="JSMeetupReactView"
/>;
};
}
const JSMeetupView = requireNativeComponent('JSMeetupView', JSMeetupReactView, {
nativeOnly: { onChange: true }
});
module.exports = JSMeetupReactView;
public class JSMeetupView extends AnyComponent implements LifecycleEventListener {
public JSMeetupView(ReactApplicationContext context) {
super(context);
context.addLifecycleEventListener(this);
}
public void setInputUrl(String inputUrl) {
...
}
public void blah() {
...
}
}
public class JSMeetupViewManager extends SimpleViewManager<JSMeetupView> {
private JSMeetupView view;
public JSMeetupViewManager(ReactApplicationContext reactContext) {
view = new JSMeetupView(reactContext);
}
@Override
public String getName() {
return "JSMeetup";
}
@Override
public JSMeetupView createViewInstance(ThemedReactContext reactContext) {
return view;
}
@ReactProp(name = "inputUrl")
public void setInputUrl(JSMeetupView view, @Nullable String inputUrl) {
view.setInputUrl(inputUrl);
}
@Override
public void receiveCommand(JSMeetupView root, int commandId, @Nullable ReadableArray args) {
switch (commandId) {
case "blah":
root.blah();
break;
}
}
}
public class MainApplication extends Application implements ReactApplication {
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
@Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new MeetupPackage() // this line !
);
}
};
@Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment