Skip to content

feat: Add getConcretePropsShared() #48710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

mrousavy
Copy link
Contributor

Summary:

Adds getConcretePropsShared() to ConcreteShadowNode.h.

I need this in Nitro Views to update state in-place without throwing away the old state object and creating a new one each time.

From reading the code it seems like you use this pattern a lot tho where you create new State objects each time - so let me know if my thing is a bad idea..

Changelog:

  • [INTERNAL] [ADDED] Add getConcretePropsShared() to ConcreteShadowNode.h

Test Plan:

Use in Nitro

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 15, 2025
@mrousavy
Copy link
Contributor Author

(not yet tested, if you think its a good idea I'll test this better)

@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Jan 15, 2025
@javache
Copy link
Member

javache commented Jan 16, 2025

I need this in Nitro Views to update state in-place without throwing away the old state object and creating a new one each time.

Could you explain a bit more what you're trying to do here? Maybe share how you'd this API in Nitro Views?

Generally, this API is fine, as we already expose the same as getProps, which you can then cast yourself.

@mrousavy
Copy link
Contributor Author

mrousavy commented Jan 21, 2025

Could you explain a bit more what you're trying to do here? Maybe share how you'd this API in Nitro Views?

Sure! So in Nitro Views, I have my own Fabric component, and because of how React Android works, there was no direct way to access the Android View through the C++ react::ConcreteViewShadowNode (or ConcreteComponentDescriptor/ViewProps/...whatever).

So a working solution we found instead was to use C++ State (the last type argument for react::ConcreteViewShadowNode<...>), which internally just holds the props. (See this code where I do that)

This C++ state will be passed to Java using StateWrapper/StateWrapperImpl.
In my Java ViewManager I can override the updateState(...) function which receives said StateWrapper.

Then in there, I call a JNI function which unwraps the StateWrapperImpl, gets the concrete state (my custom C++ State type), and then gets the props off of that.

Now to the problem: The props this C++ State is holding is a copy of the C++ Props I have (see this), and not a std::shared_ptr of the C++ Props. Why? Well, because ConcreteShadowNode only has getProps(): Props, and not getPropsShared(): shared_ptr<Props>. I thought this was an unnecessary copy, so this is purely a performance thing.
Hence this PR.

But right now I am "blocked" by something else; react-native has special parsing for function props to convert them into events. The native side just receives a boolean (jsi::Value(true)) instead of the jsi::Function. We submitted PRs for that as well; #48777 & facebook/react#32119 - those seem to resolve the issue and Nitro Views can render :)

@facebook-github-bot
Copy link
Contributor

@javache has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Jan 22, 2025
@facebook-github-bot
Copy link
Contributor

@javache merged this pull request in f830f2c.

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @mrousavy in f830f2c

When will my fix make it into a release? | How to file a pick request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants