Files
react-native/React/Fabric/Mounting/UIView+ComponentViewProtocol.mm
Valentin Shergin c79db53a9a Fabric: Introducing -[RCTComponentViewProtocol finalizeUpdates:]
Summary:
The new method is being called right after all update methods were called for a particular component view.
It is useful for performing updates that require knowledge of several independent aspects of the compound mounting change (e.g. props *and* layout constraints).

Reviewed By: JoshuaGross

Differential Revision: D14896801

fbshipit-source-id: 485d8c97d81d7a2ad7a7afc209094c328da6ef3c
2019-04-16 07:35:07 -07:00

102 lines
3.3 KiB
Plaintext

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "UIView+ComponentViewProtocol.h"
#import <React/RCTAssert.h>
#import <React/RCTLog.h>
#import <React/RCTUtils.h>
#import "RCTConversions.h"
using namespace facebook::react;
@implementation UIView (ComponentViewProtocol)
- (void)mountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index
{
[self insertSubview:childComponentView atIndex:index];
}
- (void)unmountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index
{
RCTAssert(childComponentView.superview == self, @"Attempt to unmount improperly mounted component view.");
[childComponentView removeFromSuperview];
}
- (void)updateProps:(SharedProps)props oldProps:(SharedProps)oldProps
{
// Default implementation does nothing.
}
- (void)updateEventEmitter:(SharedEventEmitter)eventEmitter
{
// Default implementation does nothing.
}
- (void)updateLocalData:(SharedLocalData)localData oldLocalData:(SharedLocalData)oldLocalData
{
// Default implementation does nothing.
}
- (void)updateState:(facebook::react::State::Shared)state oldState:(facebook::react::State::Shared)oldState
{
// Default implementation does nothing.
}
- (void)updateLayoutMetrics:(LayoutMetrics)layoutMetrics oldLayoutMetrics:(LayoutMetrics)oldLayoutMetrics
{
if (layoutMetrics.frame != oldLayoutMetrics.frame) {
CGRect frame = RCTCGRectFromRect(layoutMetrics.frame);
if (std::isnan(frame.origin.x) || std::isnan(frame.origin.y) || std::isnan(frame.size.width) ||
std::isnan(frame.size.height) || std::isinf(frame.origin.x) || std::isinf(frame.origin.y) ||
std::isinf(frame.size.width) || std::isinf(frame.size.height)) {
// CALayer will crash if we pass NaN or Inf values.
// It's unclear how to detect this case on cross-platform manner holistically, so we have to do it on the mounting
// layer as well. NaN/Inf is a kinda valid result of some math operations. Even if we can (and should) detect (and
// report early) incorrect (NaN and Inf) values which come from JavaScript side, we sometimes cannot backtrace the
// sources of a calculation that produced an incorrect/useless result.
RCTLogWarn(
@"-[UIView(ComponentViewProtocol) updateLayoutMetrics:oldLayoutMetrics:]: Received invalid layout metrics (%@) for a view (%@).",
NSStringFromCGRect(frame),
self);
return;
}
self.frame = frame;
}
if (layoutMetrics.layoutDirection != oldLayoutMetrics.layoutDirection) {
self.semanticContentAttribute = layoutMetrics.layoutDirection == LayoutDirection::RightToLeft
? UISemanticContentAttributeForceRightToLeft
: UISemanticContentAttributeForceLeftToRight;
}
if (layoutMetrics.displayType != oldLayoutMetrics.displayType) {
self.hidden = layoutMetrics.displayType == DisplayType::None;
}
}
- (void)finalizeUpdates:(RNComponentViewUpdateMask)updateMask
{
// Default implementation does nothing.
}
- (void)prepareForRecycle
{
// Default implementation does nothing.
}
- (facebook::react::SharedProps)props
{
RCTAssert(NO, @"props access should be implemented by RCTViewComponentView.");
return nullptr;
}
@end