-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sample component with state in RNTester (#34909)
Summary: Pull Request resolved: #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
- Loading branch information
1 parent
d7c4136
commit 1a9cceb
Showing
14 changed files
with
520 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
packages/rn-tester/NativeComponentWithState/NativeComponentWithState.podspec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# 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. | ||
|
||
require "json" | ||
|
||
package = JSON.parse(File.read(File.join(__dir__, "../" "package.json"))) | ||
|
||
Pod::Spec.new do |s| | ||
s.name = "NativeComponentWithState" | ||
s.version = package["version"] | ||
s.summary = package["description"] | ||
s.description = "native-component-with-state" | ||
s.homepage = "https://github.com/sota000/my-native-view.git" | ||
s.license = "MIT" | ||
s.platforms = { :ios => "12.4", :tvos => "12.4" } | ||
s.compiler_flags = '-Wno-documentation -Wno-nullability-completeness' | ||
s.author = "Facebook, Inc. and its affiliates" | ||
s.source = { :git => "https://github.com/facebook/my-native-view.git", :tag => "#{s.version}" } | ||
s.pod_target_xcconfig = { | ||
"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/RCT-Folly\" \"$(PODS_ROOT)/boost\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-Codegen/React_Codegen.framework/Headers\"", | ||
"CLANG_CXX_LANGUAGE_STANDARD" => "c++17" | ||
} | ||
|
||
s.source_files = "ios/**/*.{h,m,mm,cpp}" | ||
s.requires_arc = true | ||
|
||
install_modules_dependencies(s) | ||
|
||
# Enable codegen for this library | ||
use_react_native_codegen!(s, { | ||
:library_name => "NativeComponentWithStateSpec", | ||
:react_native_path => "../../../", | ||
:js_srcs_dir => "./js", | ||
:library_type => "components" | ||
}) | ||
end |
45 changes: 45 additions & 0 deletions
45
...ester/NativeComponentWithState/ios/RNTNativeComponentWithStateCustomComponentDescriptor.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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 <react/renderer/core/ConcreteComponentDescriptor.h> | ||
#include "RNTNativeComponentWithStateCustomShadowNode.h" | ||
|
||
namespace facebook { | ||
namespace react { | ||
|
||
/* | ||
* Descriptor for <RNTNativeComponentWithStateCustomComponentDescriptor> | ||
* component. | ||
*/ | ||
class RNTNativeComponentWithStateCustomComponentDescriptor final | ||
: public ConcreteComponentDescriptor< | ||
RNTNativeComponentWithStateCustomShadowNode> { | ||
public: | ||
RNTNativeComponentWithStateCustomComponentDescriptor( | ||
ComponentDescriptorParameters const ¶meters) | ||
: ConcreteComponentDescriptor(parameters), | ||
imageManager_(std::make_shared<ImageManager>(contextContainer_)) {} | ||
|
||
void adopt(ShadowNode::Unshared const &shadowNode) const override { | ||
ConcreteComponentDescriptor::adopt(shadowNode); | ||
|
||
auto compShadowNode = | ||
std::static_pointer_cast<RNTNativeComponentWithStateCustomShadowNode>( | ||
shadowNode); | ||
|
||
// `RNTNativeComponentWithStateCustomShadowNode` uses `ImageManager` to | ||
// initiate image loading and communicate the loading state | ||
// and results to mounting layer. | ||
compShadowNode->setImageManager(imageManager_); | ||
} | ||
|
||
private: | ||
const SharedImageManager imageManager_; | ||
}; | ||
|
||
} // namespace react | ||
} // namespace facebook |
61 changes: 61 additions & 0 deletions
61
...es/rn-tester/NativeComponentWithState/ios/RNTNativeComponentWithStateCustomShadowNode.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ¤tState = 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 |
60 changes: 60 additions & 0 deletions
60
...ages/rn-tester/NativeComponentWithState/ios/RNTNativeComponentWithStateCustomShadowNode.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <jsi/jsi.h> | ||
#include <react/renderer/components/AppSpecs/EventEmitters.h> | ||
#include <react/renderer/components/AppSpecs/Props.h> | ||
#include <react/renderer/components/AppSpecs/States.h> | ||
#include <react/renderer/components/view/ConcreteViewShadowNode.h> | ||
|
||
#include <react/renderer/imagemanager/ImageManager.h> | ||
#include <react/renderer/imagemanager/primitives.h> | ||
|
||
namespace facebook { | ||
namespace react { | ||
|
||
JSI_EXPORT extern const char RNTNativeComponentWithStateComponentName[]; | ||
|
||
/* | ||
* `ShadowNode` for <Slider> component. | ||
*/ | ||
class RNTNativeComponentWithStateCustomShadowNode final | ||
: public ConcreteViewShadowNode< | ||
RNTNativeComponentWithStateComponentName, | ||
RNTNativeComponentWithStateProps, | ||
RNTNativeComponentWithStateEventEmitter, | ||
RNTNativeComponentWithStateState> { | ||
public: | ||
using ConcreteViewShadowNode::ConcreteViewShadowNode; | ||
|
||
// Associates a shared `ImageManager` with the node. | ||
void setImageManager(const SharedImageManager &imageManager); | ||
|
||
static RNTNativeComponentWithStateState initialStateData( | ||
ShadowNodeFragment const &fragment, | ||
ShadowNodeFamilyFragment const &familyFragment, | ||
ComponentDescriptor const &componentDescriptor) { | ||
auto imageSource = ImageSource{ImageSource::Type::Invalid}; | ||
return {imageSource, {imageSource, nullptr}}; | ||
} | ||
|
||
#pragma mark - LayoutableShadowNode | ||
|
||
void layout(LayoutContext layoutContext) override; | ||
|
||
private: | ||
void updateStateIfNeeded(); | ||
|
||
ImageSource getImageSource() const; | ||
|
||
SharedImageManager imageManager_; | ||
}; | ||
|
||
} // namespace react | ||
} // namespace facebook |
17 changes: 17 additions & 0 deletions
17
packages/rn-tester/NativeComponentWithState/ios/RNTNativeComponentWithStateView.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#import <React/RCTViewComponentView.h> | ||
#import <UIKit/UIKit.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface RNTNativeComponentWithStateView : RCTViewComponentView | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
Oops, something went wrong.