fix: fix ref issue when transforming to Solid.js code
Description
Fix ref issue when mitosis build target is solid
Issue
I used the ref example provided on the official doc.
Playground: Click here
It is correctly transformed on React, which will present the output codes like these below:
import * as React from "react";
import { forwardRef } from "react";
const MyInput = forwardRef(function MyInput(props, inputRef) {
return <input ref={inputRef} />;
});
export default MyInput;
But, it will cause issue on Solid, and we could see the issue at console panel.
Reason
The issue is caused by the getRefsString method in Solid.js generator, which can be read here.
const getRefsString = (json: MitosisComponent, options: ToSolidOptions) =>
Array.from(getRefs(json))
.map((ref) => {
const typeParameter = (options.typescript && json['refs'][ref]?.typeParameter) || '';
return `let ${ref}${typeParameter ? ': ' + typeParameter : ''};`;
})
.join('\n');
And with the insertion ${getRefsString(json, options)} in solid code, eventaully, the generated solid.js code will become something like this:
function MyComponent(props:any) {
let props.inputRef;
return (
<input ref={props.inputRef!} />
)
}
export default MyComponent;
And when these code met with prettier format function in Solid.js generator, it will throw out the error, which leads to the break of Solid.js code bundle.
We could easily reproduce it in a online prettier editor.
Solution
Updated at May, 4th:
Remove ref definition when it's passed by props, such as props.inputRef.
const getRefsString = (json: MitosisComponent, options: ToSolidOptions) =>
Array.from(getRefs(json))
.map((ref) => {
if (ref.includes('.')) return '';
const typeParameter = (options.typescript && json['refs'][ref]?.typeParameter) || '';
return `let ${ref}${typeParameter ? ': ' + typeParameter : ''};`;
})
.join('\n');
Then it will behave in two ways:
- Ref is created by
useRef, will still remain. - Ref is passed by
props, will not remain after this PR.
~~Remove the getRefsString method calling.~~
~~Then the Solid.js generator will generae Solid.js code like these below, which works as expected and will not throw out any error.~~
function MyComponent(props:any) {
return (
<input ref={props.inputRef!} />
)
}
export default MyComponent;
🦋 Changeset detected
Latest commit: a1ca5ad5f2fd29b989a612251e4b6ef757c06183
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 2 packages
| Name | Type |
|---|---|
| @builder.io/mitosis | Patch |
| @builder.io/mitosis-cli | Patch |
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR
The latest updates on your projects. Learn more about Vercel for Git ↗︎
| Name | Status | Preview | Comments | Updated (UTC) |
|---|---|---|---|---|
| mitosis-fiddle | ✅ Ready (Inspect) | Visit Preview | 💬 Add feedback | Mar 18, 2024 11:50am |
☁️ Nx Cloud Report
CI is running/has finished running commands for commit a1ca5ad5f2fd29b989a612251e4b6ef757c06183. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.
📂 See all runs for this CI Pipeline Execution
✅ Successfully ran 4 targets
Sent with 💌 from NxCloud.
Hi, @samijaber. Could you help me with the pr test failed issue?
I've been tested on my local macOS and update snapshot, and it all worked well. But the test just failed on github action, any particular reason why? Thanks.
Same test failed issue happens on my other PR as well.
Hi, @samijaber, do you have a moment to see the reason of why vitest snapshot not correctly generated? That would be great help for my MR, thanks.
Hi @rqzheng2015 , sorry for the delay. There's an issue with our test suite causing tests to not work properly.
It is causing all PRs to break unless additional instructions are followed.
I will fix this issue first and then let you know to rerun your tests locally to get them fixed
Hi @samijaber, I have modified my PR to fix the ref issue changes and vitest snapshot issues, please check when you are free, thanks.
Thanks for the fix @rqzheng2015 🙏🏽