From ed36e896ac34fcbefece87456dbdfdff30d22ad5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hanno=20J=2E=20G=C3=B6decke?= Date: Fri, 13 Dec 2024 10:02:32 -0800 Subject: [PATCH] feat: add `getState` for `StateWrapperImpl` (#48255) Summary: We're trying to pass `jsi::Value`s directly to our view components (and convert them to java/swift types manually). That way we can pass "complex" objects to our views (such as `jsi::Object`s with `NativeState` attached, without the need to convert them to e.g. `folly::dynamic`). On android we store our complex prop values on the `StateWrapperImpl` to pass the complex types between c++ and java/kotlin. See an example here: https://github.com/hannojg/nitro/blob/2378fe7754294c496b2cbcd62f7109529e276427/packages/react-native-nitro-image/nitrogen/generated/android/c%2B%2B/JValueFromStateWrapper.cpp#L21-L23 ``` const auto& customStateData = dynamic_cast&>(state); CustomStateData data = customStateData.getData(); std::shared_ptr nativeProp = data.nativeProp; ``` > (And then it might be used in java like this:) https://github.com/hannojg/nitro/blob/2378fe7754294c496b2cbcd62f7109529e276427/packages/react-native-nitro-image/android/src/main/java/com/margelo/nitro/image/NitroExampleViewManager.java#L31-L38 ```kotlin public Object updateState(NonNull View view, ReactStylesDiffMap props, StateWrapper stateWrapper) { StateWrapperImpl stateWrapperImpl = (StateWrapperImpl) stateWrapper; HybridTestObjectSwiftKotlinSpec nativeProp = ValueFromStateWrapper.valueFromStateWrapper(stateWrapperImpl); long value = nativeProp.getBigintValue(); Log.d("NitroExampleViewManager", "Value from state: " + value); ``` For that we need to be able to access the underlying state, which is what we added in this PR. ## Changelog: [ANDROID] [ADDED] - Added `getState` method for `StateWrapperImpl` Pull Request resolved: https://github.com/facebook/react-native/pull/48255 Test Plan: Internal change, just make sure all tests are passing Reviewed By: cipolleschi Differential Revision: D67196130 Pulled By: javache fbshipit-source-id: 7da74bcddef79abd3122baaad1bfce30330ecc80 --- .../src/main/jni/react/fabric/StateWrapperImpl.cpp | 4 ++++ .../ReactAndroid/src/main/jni/react/fabric/StateWrapperImpl.h | 1 + 2 files changed, 5 insertions(+) diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/fabric/StateWrapperImpl.cpp b/packages/react-native/ReactAndroid/src/main/jni/react/fabric/StateWrapperImpl.cpp index d1af22e3642..01db7fb563d 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/fabric/StateWrapperImpl.cpp +++ b/packages/react-native/ReactAndroid/src/main/jni/react/fabric/StateWrapperImpl.cpp @@ -56,6 +56,10 @@ void StateWrapperImpl::setState(std::shared_ptr state) { state_ = state; } +std::shared_ptr StateWrapperImpl::getState() const { + return state_; +} + void StateWrapperImpl::registerNatives() { registerHybrid({ makeNativeMethod("initHybrid", StateWrapperImpl::initHybrid), diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/fabric/StateWrapperImpl.h b/packages/react-native/ReactAndroid/src/main/jni/react/fabric/StateWrapperImpl.h index e033bb1ec6b..340b7761458 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/fabric/StateWrapperImpl.h +++ b/packages/react-native/ReactAndroid/src/main/jni/react/fabric/StateWrapperImpl.h @@ -29,6 +29,7 @@ class StateWrapperImpl : public jni::HybridClass { jni::local_ref getStateDataImpl(); void updateStateImpl(NativeMap* map); void setState(std::shared_ptr state); + std::shared_ptr getState() const; private: std::shared_ptr state_;