Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ClipboardModule.java #268

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Jenish109
Copy link

Overview

The issue with your ClipboardModule.java lies in the use of the ContextBaseJavaModule class and the getContext() method, which are no longer available in the latest versions of React Native.

Steps to Fix
Replace ContextBaseJavaModule with ReactContextBaseJavaModule: ContextBaseJavaModule has been deprecated and removed. Replace it with ReactContextBaseJavaModule.

Update the Constructor: Modify the constructor to accept a ReactApplicationContext instead of a Context.

Use getReactApplicationContext() Instead of getContext(): Replace calls to getContext() with getReactApplicationContext().

Below I attached whole ClipboardModule.js
`package com.reactnativecommunity.clipboard;

import android.content.ClipboardManager;
import android.content.ClipData;
import android.content.Context;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Promise;
import com.facebook.react.module.annotations.ReactModule;

/**

  • A module that allows JS to get/set clipboard contents.
    */
    @ReactModule(name = ClipboardModule.NAME)
    public class ClipboardModule extends ReactContextBaseJavaModule {

public static final String NAME = "RNCClipboard";

public ClipboardModule(ReactApplicationContext reactContext) {
super(reactContext);
}

@OverRide
public String getName() {
return ClipboardModule.NAME;
}

private ClipboardManager getClipboardService() {
return (ClipboardManager) getReactApplicationContext().getSystemService(Context.CLIPBOARD_SERVICE);
}

@ReactMethod
public void getString(Promise promise) {
try {
ClipboardManager clipboard = getClipboardService();
ClipData clipData = clipboard.getPrimaryClip();
if (clipData != null && clipData.getItemCount() >= 1) {
ClipData.Item firstItem = clipData.getItemAt(0);
promise.resolve("" + firstItem.getText());
} else {
promise.resolve("");
}
} catch (Exception e) {
promise.reject(e);
}
}

@ReactMethod
public void setString(String text) {
ClipData clipData = ClipData.newPlainText(null, text);
ClipboardManager clipboard = getClipboardService();
clipboard.setPrimaryClip(clipData);
}

@ReactMethod
public void hasString(Promise promise) {
try {
ClipboardManager clipboard = getClipboardService();
ClipData clipData = clipboard.getPrimaryClip();
promise.resolve(clipData != null && clipData.getItemCount() >= 1);
} catch (Exception e) {
promise.reject(e);
}
}
}
`

Test Plan

@Arnab-CarePlix
Copy link

Thanks, this change fixed my build problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants