Simplify createInitialState interface (#37069)

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

changelog: [internal]

To create initial C++ state, nothing beside props is needed from `ShadowNodeFragment` and this diff removes it.

It makes creation of state easier as we will no longer need to check if props are nullptr.

Reviewed By: mdvacca

Differential Revision: D45183692

fbshipit-source-id: 81ab8eb3c57f6ff64aaed7c5b395555dce6b60b2
This commit is contained in:
Samuel Susla
2023-04-26 05:50:49 -07:00
committed by Facebook GitHub Bot
parent a718a88510
commit 2cda4f71b6
10 changed files with 13 additions and 21 deletions
@@ -136,8 +136,7 @@ ShadowNode::Shared ComponentDescriptorRegistry::createNode(
PropsParserContext{surfaceId, *contextContainer_.get()},
nullptr,
RawProps(propsDynamic));
auto const state =
componentDescriptor.createInitialState(ShadowNodeFragment{props}, family);
auto const state = componentDescriptor.createInitialState(props, family);
return componentDescriptor.createShadowNode(
{
@@ -41,7 +41,7 @@ class ImageShadowNode final : public ConcreteViewShadowNode<
void setImageManager(const SharedImageManager &imageManager);
static ImageState initialStateData(
ShadowNodeFragment const &fragment,
Props::Shared const &props,
ShadowNodeFamilyFragment const &familyFragment,
ComponentDescriptor const &componentDescriptor) {
auto imageSource = ImageSource{ImageSource::Type::Invalid};
@@ -49,16 +49,10 @@ void ScrollViewShadowNode::updateScrollContentOffsetIfNeeded() {
}
ScrollViewState ScrollViewShadowNode::initialStateData(
const ShadowNodeFragment &fragment,
Props::Shared const &props,
const ShadowNodeFamilyFragment & /*familyFragment*/,
const ComponentDescriptor & /*componentDescriptor*/) {
if (fragment.props != ShadowNodeFragment::propsPlaceholder()) {
auto const &scrollViewProps =
static_cast<ScrollViewProps const &>(*fragment.props);
return {scrollViewProps.contentOffset, {}, 0};
} else {
return ScrollViewState{};
}
return {static_cast<ScrollViewProps const &>(*props).contentOffset, {}, 0};
}
#pragma mark - LayoutableShadowNode
@@ -29,7 +29,7 @@ class ScrollViewShadowNode final : public ConcreteViewShadowNode<
using ConcreteViewShadowNode::ConcreteViewShadowNode;
static ScrollViewState initialStateData(
ShadowNodeFragment const &fragment,
Props::Shared const &props,
ShadowNodeFamilyFragment const &familyFragment,
ComponentDescriptor const &componentDescriptor);
@@ -34,7 +34,7 @@ class AndroidTextInputComponentDescriptor final
}
virtual State::Shared createInitialState(
ShadowNodeFragment const &fragment,
Props::Shared const &props,
ShadowNodeFamily::Shared const &family) const override {
int surfaceId = family->getSurfaceId();
@@ -121,7 +121,7 @@ class ComponentDescriptor {
* State's data which can be constructed based on initial Props.
*/
virtual State::Shared createInitialState(
ShadowNodeFragment const &fragment,
Props::Shared const &props,
ShadowNodeFamily::Shared const &family) const = 0;
/*
@@ -151,7 +151,7 @@ class ConcreteComponentDescriptor : public ComponentDescriptor {
};
virtual State::Shared createInitialState(
ShadowNodeFragment const &fragment,
Props::Shared const &props,
ShadowNodeFamily::Shared const &family) const override {
if (std::is_same<ConcreteStateData, StateData>::value) {
// Default case: Returning `null` for nodes that don't use `State`.
@@ -161,7 +161,7 @@ class ConcreteComponentDescriptor : public ComponentDescriptor {
return std::make_shared<ConcreteState>(
std::make_shared<ConcreteStateData const>(
ConcreteShadowNode::initialStateData(
fragment, ShadowNodeFamilyFragment::build(*family), *this)),
props, ShadowNodeFamilyFragment::build(*family), *this)),
family);
}
@@ -86,7 +86,7 @@ class ConcreteShadowNode : public BaseShadowNodeT {
}
static ConcreteStateData initialStateData(
ShadowNodeFragment const &fragment,
Props::Shared const & /*props*/,
ShadowNodeFamilyFragment const &familyFragment,
ComponentDescriptor const &componentDescriptor) {
return {};
@@ -31,8 +31,8 @@ ShadowNode::Unshared ComponentBuilder::build(
elementFragment.tag, elementFragment.surfaceId, nullptr},
nullptr);
auto initialState = componentDescriptor.createInitialState(
ShadowNodeFragment{elementFragment.props}, family);
auto initialState =
componentDescriptor.createInitialState(elementFragment.props, family);
auto constShadowNode = componentDescriptor.createShadowNode(
ShadowNodeFragment{
@@ -80,8 +80,7 @@ ShadowNode::Shared UIManager::createNode(
componentDescriptor.createFamily(fragment, std::move(eventTarget));
auto const props =
componentDescriptor.cloneProps(propsParserContext, nullptr, rawProps);
auto const state =
componentDescriptor.createInitialState(ShadowNodeFragment{props}, family);
auto const state = componentDescriptor.createInitialState(props, family);
auto shadowNode = componentDescriptor.createShadowNode(
ShadowNodeFragment{