Skip to content
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

Add refactoring action to export selected JSX code as stateless component #15090

Open
rosieks opened this issue Apr 9, 2017 · 5 comments
Open
Labels
Domain: Refactorings e.g. extract to constant or function, rename symbol Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Milestone

Comments

@rosieks
Copy link

rosieks commented Apr 9, 2017

Please add refactoring action that will improve productivity while working with JSX/TSX. Suppose that I have the following code:

class Page extends React.Component {
    render() {
        return (
            <div>
                <h1>{this.props.header}</h1>
            </div>
        );
    }
}

I would like to select line with <h1> and chose from context menu "Extract to stateless component". And that would create the following stateless component:

const NewComponent = ({ header }) => (<h1>{header}</h1>);

The selected text would be replaced with that:

<NewComponent header={this.props.header} />
@cevek
Copy link

cevek commented Apr 9, 2017

It would also be fine if it can convert component class into stateless component if possible and reverse convertion

class Page extends React.Component<PageProps, {}> {
    render() {
        return (
            <div>
                <h1>{this.props.header}</h1>
            </div>
        );
    }
}

// refactor converts to 

function Page({header}: PageProps) {
    return (
             <div>
                <h1>{header}</h1>
            </div>
    );
}

I think it possible to do with language service's plugin
https://github.com/RyanCavanaugh/sample-ts-plugin

@mhegazy mhegazy added Suggestion An idea for TypeScript Domain: Refactorings e.g. extract to constant or function, rename symbol labels Apr 21, 2017
@DanielRosenwasser
Copy link
Member

I have a partially working version of this, but it was hard to get this working without #15868. The problem was accomodating propTypes.

@rosieks
Copy link
Author

rosieks commented Aug 11, 2017

@DanielRosenwasser - is it because after extracting selected text to stateless function you also want to generate propTypes? If so then maybe it would be nice to generate that without propTypes - as far as I know it's not necessary to have it

@weswigham
Copy link
Member

This should no longer be blocked, so if you already have a quickfix, let's see it. 😄

@RyanCavanaugh RyanCavanaugh removed the In Discussion Not yet reached consensus label Jul 29, 2019
@OliverJAsh
Copy link
Contributor

This is very similar to #25891.

I made a proposal here: #25891 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Refactorings e.g. extract to constant or function, rename symbol Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

7 participants