-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Feat: Extract function with jsx selection should be replaced with jsx sfc #25891
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
Comments
I would say a new refactoring should be offered in this context to extract the JSX element to an SFC. |
But source code of new refactoring will be almost the same as main extractFunction refactoring, isn't it? Or we can reuse existent code? |
This has nothing to do with the source code. the same refactoring implementation can offer multiple refactorings to the user. for instance extractFunction offers one for every containing scope. it is still the same implementation, it just gives the user choices. |
I think this would be more obvious and useful when selecting a chunk of jsx, for example: function Component({title, subtitle, author, publishedDate, text}){
return (
<div>
<h1>{title}</h1>
<h2>{subtlitle}</h2>
<div className="byline">
<a href={author.url}>{author.name}</a> ({publishedDate})
</div>
<p>{text}</p>
</div>
)
} When selecting and extracting the function Component({title, subtitle, author, publishedDate, text}){
return (
<div>
<h1>{title}</h1>
<h2>{subtlitle}</h2>
- <div className="byline">
- <a href={author.url}>{author.name}</a> ({publishedDate})
- </div>
+ <Byline author={author} publishedDate={publishedDate} />
<p>{text}</p>
</div>
)
} For this to work correctly it's necessary to make the function accept only a single parameter, so that it can be sent in as the props. That is, the implementation would in the above scenario be function Byline({author, publishedDate}){
return (
<div className="byline">
<a href={author.url}>{author.name}</a> ({publishedDate})
</div>
)
} |
Perhaps we could achieve this by adding a few smaller refactorings and then composing them together to create a larger refactoring.
Here's a video where I demonstrate this using the example above: https://www.loom.com/share/4498fb6487bf42c39aa59a4a52dee5a1 The first 2 refactorings already exist, so we'd just need to add the third one ("Convert call expression to JSX element"). (I have written a version of this myself which I'm happy to share if anyone is interested.) Once we have that, TypeScript could (somehow) package up these refactorings into a larger one and call it something like "Extract JSX as component". At my workplace we prefer to write components using function expressions instead of function declarations. This has a few extra steps:
|
Note this is very similar to #15090. |
Suggestion
If we select jsx element and do extract refactor, editor should be refactor it in jsx maner
Examples
After refactor should be
Current behavior
The text was updated successfully, but these errors were encountered: