Arguments
from React Native Bridge has a helper method Arguments.fromList(list)
that returns a WritableArray
, which is a subclass of ReadableArray
, so satisfies the condition and is ready to pass to putArray
.
For example, to take a list and push it to a JS event as an array property:
Kotlin:
import com.facebook.react.bridge.Arguments
// ...
val params = Arguments.createMap().apply {
putArray("data", Arguments.fromList(someList))
// ...put more params
}
Java:
import com.facebook.react.bridge.Arguments;
// ...
WriteableArray array = Arguments.fromList(someList);
WritableMap params = Arguments.createMap();
params.putArray(array);
// ...put more params
Pass these to .emit("SomeEvent", params)
and JS should receive { data: [...someList] }
.
There's also Arguments.makeNativeArray(list)
, which has an almost identical API and implementation to .fromList
- both create a WritableArray
by iterating a list arg, pushing each item by type. The only differences I can see are:
.fromList
explicitly handles nested arrays
.makeNativeArray
explicitly handles null
(returning an empty array)