-
Notifications
You must be signed in to change notification settings - Fork 83
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
Enhance Handling of F# Record Types as Props in ReactComponent Attribute #603
Comments
@lukaszkrzywizna Just to be sure I am reading correctly, in the pictures below the normal functions is without the
I believe this solution would be the best as it requires no knowledge from the user. It would also mean that if F# says this API is available then it will works as expected etc. However, we need to check if passing a pure Fable record works for react and don't have limitations. For example, will React still be able to diff the properties correctly even if they are 2 levels deep etc. Does this works with record decorated with Regarding One problem, I could see with this changes is if people use F# to write their component and consume them from JS/TS. Then, they would need to build the properties using the Record constructor exposed by Fable to make sure they have the expected type instance. |
A normal function means a function without the
I’ve prepared a new version with automatic wrapping, and so far everything works. It wraps only declared-type records (anonymous records are still processed in the same way). Check the #606.
Thanks for the suggestion, I’ll check this. |
Thinking about this issue and PR, there is actually 1 drawbacks in rewritings if the user use a record: type MyProps =
{
value: string
key: string
} Then the code seen by react with be: createElement(Component, { props = { value: "", key: "" } }) This is problematic because now they component is not anymore optimised by with the It looks like the current implementation is already making some assumptions on the Feliz/Feliz.CompilerPlugins/ReactComponent.fs Lines 81 to 84 in 4379c03
I think to work around the issue, I pointed out we should also detect if the record has a createElement(Component, { props = { value: "", key: "" }, key = "" }) This way we can keep both the Record prototype and the key diffing optimisation. |
@MangelMaxime I've added support for lowercased 'key' property. |
Issue:
When using Feliz with F# and Fable, passing F# record types as props in React components results in unexpected behavior. The record type, which Fable translates into a JavaScript object with a specific prototype, loses its prototype once processed by React.createElement. This leads to the absence of expected methods and properties on the prototype.
Illustration of simple record transformation:
Differences in objects with and without the ReactComponent attribute:
Current Workaround by Users:
Currently, a practical workaround for users is to manually wrap the record into an anonymous object. This method involves encapsulating the record as a single property within an anonymous props object, which preserves the prototype through the React component lifecycle.
Example of wrapping the record:
Proposed Solution for Feliz:
I propose that Feliz should either:
@Zaid-Ajaj @MangelMaxime I'm interested in what you think.
The text was updated successfully, but these errors were encountered: