10

I am trying to call a native ReactMethod from react native but it is throwing the following error:

C++ Exception in 'NativeModules': java.lang.RuntimeException: Got unknown argument class: Object

This is how I have the method defined in android:

@ReactMethod
public void setItem(String key, Object value) {
    // code
}

This is how i am calling this method:

MyModule.setItem("mykey", "1234567890");

I wonder if ReactMethod supports object type as parameter or not? Because I want to set any type of item.

2 Answers 2

15

According to the documentation, the following argument types are supported for methods annotated with @ReactMethod. The argument types directly map to their JavaScript equivalents.

Boolean -> Bool
Integer -> Number
Double -> Number
Float -> Number
String -> String
Callback -> function
ReadableMap -> Object
ReadableArray -> Array

In your case you could use a ReadableMap as argument, which maps on the JavaScript Object type.

Sign up to request clarification or add additional context in comments.

2 Comments

ReadableMap is for an associative array, thus it cannot be used for the other types.
was getting ReadableNativeArray cannot be cast to * from react, after hours understood that we have to use ReadableArray instead ReadableNativeArray, react errors are useless and misleading
1

You can use Dynamic type, which is actually not documented.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.