Adjust logic responsible for cloning elements #19
+8
−12
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello, I have another thing that I stumbled upon while playing with Restyle 😃
Current check verifies only whether something is string and in that cases it avoids cloning. However, besides strings there're many things that are supported as renderable node in React. That makes code like this throw error:
And like this with React 19 (context, promises, generators, async iterables are renderable nodes now):
This is the error:
I've tried to investigate React source code what's the best way to differentiate whether that's something "renderable" by React, but that logic is pretty convoluted.
I've came to idea to do it the other way and differentiate whether something is clonable, because that function logic is much easier. I implemented it as a simple check whether something is object and whether has
type
property. This is still not perfect and I'm open for better ideas, but I feel like it's much better than the current one and will only fail in cases where built-in JSX transform would also fail (but at different place). I've also looked through and it seems like thetype
isn't someimplementation details
but it's official API.I also have another idea, maybe it’s worth using
react-is
here?My current workaround is to just add "space" after the node, so it's handled as array 😅
Also when applying my patch as change in my project and test-driving it, I noticed one more thing (see difference between 1st and 2nd commit).
Stuff like this still wasn't properly rendered because of truthy check:
So I adjusted also the truthiness check and I try to mostly give the job of avoiding falsy values back to React