Add sample component with state in RNTester (#34909)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/34909

This Diff introduces a Sample component in RNTester which uses the NativeState to load some images.
It is an example on how to use CustomNativeState

In this first diff, I focused on the iOS side of things. The next diff will make this work with Android.

## Changelog
[iOS][Added] - Introduce sample component which work with the native state.

Reviewed By: cortinico

Differential Revision: D39884926

fbshipit-source-id: 9323d751fd06a1bb8ff93af836d97010c2095833
This commit is contained in:
Riccardo Cipolleschi authored and Facebook GitHub Bot committed 2022-10-10 02:51:06 -07:00
1 parent d7c41361dd
commit 1a9cceb20b
14 files changed
+520 -2

No files matched your search

@@ -0,0 +1,61 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "RNTNativeComponentWithStateCustomShadowNode.h"
#include <react/renderer/core/LayoutContext.h>
namespace facebook {
namespace react {
extern const char RNTNativeComponentWithStateComponentName[] =
"RNTNativeComponentWithState";
void RNTNativeComponentWithStateCustomShadowNode::setImageManager(
const SharedImageManager &imageManager) {
ensureUnsealed();
imageManager_ = imageManager;
}
void RNTNativeComponentWithStateCustomShadowNode::updateStateIfNeeded() {
const auto &newImageSource = getImageSource();
auto const &currentState = getStateData();
auto imageSource = currentState.getImageSource();
bool anyChanged = newImageSource != imageSource;
if (!anyChanged) {
return;
}
// Now we are about to mutate the Shadow Node.
ensureUnsealed();
// It is not possible to copy or move image requests from SliderLocalData,
// so instead we recreate any image requests (that may already be in-flight?)
// TODO: check if multiple requests are cached or if it's a net loss
auto state = RNTNativeComponentWithStateState{
newImageSource,
imageManager_->requestImage(newImageSource, getSurfaceId())};
setStateData(std::move(state));
}
ImageSource RNTNativeComponentWithStateCustomShadowNode::getImageSource()
const {
return getConcreteProps().imageSource;
}
void RNTNativeComponentWithStateCustomShadowNode::layout(
LayoutContext layoutContext) {
updateStateIfNeeded();
ConcreteViewShadowNode::layout(layoutContext);
}
} // namespace react
} // namespace facebook