Skip to content

Commit 20ce890

Browse files
committed
Add example
1 parent 4c2a499 commit 20ce890

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed

README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
This codemod automatically transforms **React class components** into **React functional components using Hooks** for you!
44

5+
|Before|After|
6+
|---|---|
7+
|![before example 1](./img/example1-before.png)|![after example 1](./img/example1-after.png)|
8+
59
## Features
610

711
- ✅ Supports props, states, methods, and refs.
@@ -27,6 +31,81 @@ npx codemod --plugin react-declassify 'src/**/*.tsx'
2731

2832
Before:
2933

34+
```tsx
35+
import React from "react";
36+
37+
type Props = {
38+
by: number;
39+
};
40+
41+
type State = {
42+
counter: number;
43+
};
44+
45+
export class C extends React.Component<Props, State> {
46+
static defaultProps = {
47+
by: 1
48+
};
49+
50+
constructor(props) {
51+
super(props);
52+
this.state = {
53+
counter: 0
54+
};
55+
}
56+
57+
render() {
58+
return (
59+
<>
60+
<button onClick={() => this.onClick()}>
61+
{this.state.counter}
62+
</button>
63+
<p>Current step: {this.props.by}</p>
64+
</>
65+
);
66+
}
67+
68+
onClick() {
69+
this.setState({ counter: this.state.counter + this.props.by });
70+
}
71+
}
72+
```
73+
74+
After:
75+
76+
```tsx
77+
import React from "react";
78+
79+
type Props = {
80+
by: number;
81+
};
82+
83+
type State = {
84+
counter: number;
85+
};
86+
87+
export const C: React.FC<Props> = props => {
88+
const {
89+
by = 1
90+
} = props;
91+
92+
const [counter, setCounter] = React.useState<number>(0);
93+
94+
function onClick() {
95+
setCounter(counter + by);
96+
}
97+
98+
return <>
99+
<button onClick={() => onClick()}>
100+
{counter}
101+
</button>
102+
<p>Current step: {by}</p>
103+
</>;
104+
};
105+
```
106+
107+
Before:
108+
30109
```jsx
31110
import React from "react";
32111

img/example1-after.png

228 KB
Loading

img/example1-before.png

274 KB
Loading

src/index.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,79 @@ describe("react-declassify", () => {
940940
});
941941

942942
test("readme example 1", () => {
943+
const input = dedent`\
944+
import React from "react";
945+
946+
type Props = {
947+
by: number;
948+
};
949+
950+
type State = {
951+
counter: number;
952+
};
953+
954+
export class C extends React.Component<Props, State> {
955+
static defaultProps = {
956+
by: 1
957+
};
958+
959+
constructor(props) {
960+
super(props);
961+
this.state = {
962+
counter: 0
963+
};
964+
}
965+
966+
render() {
967+
return (
968+
<>
969+
<button onClick={() => this.onClick()}>
970+
{this.state.counter}
971+
</button>
972+
<p>Current step: {this.props.by}</p>
973+
</>
974+
);
975+
}
976+
977+
onClick() {
978+
this.setState({ counter: this.state.counter + this.props.by });
979+
}
980+
}
981+
`;
982+
const output = dedent`\
983+
import React from "react";
984+
985+
type Props = {
986+
by: number;
987+
};
988+
989+
type State = {
990+
counter: number;
991+
};
992+
993+
export const C: React.FC<Props> = props => {
994+
const {
995+
by = 1
996+
} = props;
997+
998+
const [counter, setCounter] = React.useState<number>(0);
999+
1000+
function onClick() {
1001+
setCounter(counter + by);
1002+
}
1003+
1004+
return <>
1005+
<button onClick={() => onClick()}>
1006+
{counter}
1007+
</button>
1008+
<p>Current step: {by}</p>
1009+
</>;
1010+
};
1011+
`;
1012+
expect(transform(input, { ts: true })).toBe(output);
1013+
});
1014+
1015+
test("readme example 2", () => {
9431016
const input = dedent`\
9441017
import React from "react";
9451018

0 commit comments

Comments
 (0)