Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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;
/**
*/
@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