Codemod commands for everyday work with React. All commands support Flow, TypeScript, and plain JS.
Adds a parent JSX Element around the selected JSX node(s) or the node that contains the cursor.
const Foo = () => (
<div>
// selection start
{foo}
{bar}
<span />
// selection end
{baz}
</div>
)
const Foo = () => (
<div>
<Test>
{foo}
{bar}
<span />
</Test>
{baz}
</div>
)
Wraps the JSX Element that contains the cursor in a parent JSX Element with a child function (making the child function return the wrapped element).
const Foo = () => (
<div>
<Bar />
</div>
)
Position cursor in <Bar />
then run the command.
const Foo = () => (
<div>
<Test>{(): React.ReactNode => <Bar />}</Test>
</div>
)
Adds the identifier under the cursor as a prop to the surrounding component.
Adds a prop type declaration if possible, and binds the identifier via destructuring on props
or replaces it with a reference to props
/this.props
.
import * as React from 'react'
interface Props {}
const Foo = (props: Props) => <div>{text}</div>
Position cursor in the middle of text
and then run the command.
The command will prompt you what type to use for the property, enter string
for example:
import * as React from 'react'
interface Props {
text: string
}
const Foo = (props: Props) => <div>{props.text}</div>
Wraps the selected JSX in {true && ...}
. If
there are multiple siblings selected, wraps in {true && <React.Fragment>...</React.Fragment>}
.
If you want to wrap in a ternary conditional like Glean's
"Render Conditionally" refactor, see wrapWithTernaryConditional
.
const Foo = () => (
<div>
{foo} bar
<span />
{baz}
</div>
)
Select from before {foo}
to before {baz}
, then run the command.
const Foo = () => (
<div>
{true && (
<React.Fragment>
{foo} bar
<span />
</React.Fragment>
)}
{baz}
</div>
)
Wraps the selected JSX in {true ? ... : null}
. If
there are multiple siblings selected, wraps in {true ? <React.Fragment>...</React.Fragment> : null}
.
const Foo = () => (
<div>
{foo} bar
<span />
{baz}
</div>
)
Select from before {foo}
to before {baz}
, then run the command.
const Foo = () => (
<div>
{true ? (
<React.Fragment>
{foo} bar
<span />
</React.Fragment>
) : null}
{baz}
</div>
)