Skip to content

Commit 82a7d68

Browse files
committed
docs: migration from react
1 parent f5dd040 commit 82a7d68

File tree

1 file changed

+141
-4
lines changed

1 file changed

+141
-4
lines changed

docs/introduction/migration.md

+141-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
## Migration from `vue-jsx`
44

5-
1. Doesn't support hyphenated prop name and hyphenated component name.
6-
2. `v-model` doesn't support Array Expression, use `v-model:$name$_trim={foo}` instead.
7-
3. Doesn't support `v-models` directive.
5+
1. Don't support hyphenated prop name and hyphenated component name.
6+
2. `v-model` don't support Array Expression, use `v-model:$name$_trim={foo}` instead.
7+
3. Don't support `v-models` directive.
88
4. Destructing props:
99

1010
> [!CAUTION]
@@ -71,4 +71,141 @@ export default () => {
7171
const foo = ref('foo')
7272
return <Comp foo={foo.value} />
7373
}
74-
```
74+
```
75+
76+
## Migration from `react`
77+
78+
Suggest using the ESLint plugin [eslint-plugin-react2vue](https://github.com/zhiyuanzmj/eslint-plugin-react2vue) for converting the React Hooks API to the Vue Composition API and Macros.
79+
80+
### useState
81+
82+
```ts
83+
// before
84+
const [foo, setFoo] = useState(count)
85+
console.log([foo, setFoo(1), setFoo])
86+
87+
// after
88+
const foo = ref(0)
89+
console.log([foo.value, foo.value = 1, val => foo.value = val])
90+
```
91+
92+
### useEffect
93+
94+
Use `watchEffect` instead of `useEffect`.
95+
96+
```ts
97+
// before
98+
useEffect(() => {
99+
console.log(foo)
100+
}, [foo])
101+
102+
// after
103+
watchEffect(() => {
104+
console.log(foo)
105+
})
106+
```
107+
108+
### useMemo
109+
110+
Use `computed` instead of `useMemo`.
111+
112+
```ts
113+
// before
114+
const double = useMemo(() => foo * 2, [foo])
115+
console.log({ double }, [double])
116+
117+
// after
118+
const double = computed(() => foo * 2)
119+
console.log({ double: double.value }, [double.value])
120+
```
121+
122+
### defineComponent
123+
124+
Use `defineComponent` macro to support destructuring props.
125+
126+
```tsx
127+
// before
128+
const Comp = ({ count = 1 }) => {
129+
return <div>{count}</div>
130+
}
131+
132+
// after
133+
const Comp = defineComponent(({ count = 1 }) => {
134+
return <div>{count}</div>
135+
})
136+
```
137+
138+
### defineSlots
139+
140+
Use `defineSlots` instead of `children` prop.
141+
142+
```tsx
143+
// before
144+
const Comp = ({ children }) => {
145+
return children
146+
}
147+
148+
// after
149+
const Comp = ({ children }) => {
150+
const slots = defineSlots()
151+
return <slots.default />
152+
}
153+
```
154+
155+
### useCallback
156+
157+
Remove useCallback.
158+
159+
```ts
160+
// before
161+
const callback = useCallback(() => {
162+
console.log(foo)
163+
}, [foo])
164+
165+
// after
166+
const callback = () => {
167+
console.log(foo)
168+
}
169+
```
170+
171+
### forwardRef
172+
173+
Remove forwardRef.
174+
175+
```tsx
176+
// before
177+
const Comp = forwardRef(({ count }, ref) => {
178+
return <div>{count}</div>
179+
})
180+
181+
// after
182+
const Comp = ({ count }) => {
183+
return <div>{count}</div>
184+
}
185+
```
186+
187+
### useImperativeHandle
188+
189+
Use `defineExpose` instead of `useImperativeHandle`.
190+
191+
```tsx
192+
// before
193+
const Comp = ({ count, ref }) => {
194+
useImperativeHandle(ref, () => {
195+
return {
196+
count: count * 2
197+
}
198+
}, [count])
199+
return <div>{count}</div>
200+
}
201+
202+
// after
203+
const Comp = ({ count }) => {
204+
defineExpose(computed(() => {
205+
return {
206+
count: count * 2
207+
}
208+
}))
209+
return <div>{count}</div>
210+
}
211+
```

0 commit comments

Comments
 (0)