Skip to content

Commit

Permalink
refactor: get rid off deprecated useWorkletCallback (#290)
Browse files Browse the repository at this point in the history
## 馃摐 Description

Migrated `useWorkletCallback` to `useCallback` + `worklet` directive.

## 馃挕 Motivation and Context

`useWorkletCallback` is deprecated from `3.6.1` version. Usage of
deprecated API is a risky thing, because it can be removed at any point
of time, so I migrated it back to `useCallback` + `worklet`.

Also I updated `reanimated` to `3.6.1` version and removed unused
`@ts-expect-error` directives.

Closes
#289

## 馃摙 Changelog

### JS
- updated `reanimated` to version `3.6.1`;
- `useWorkletCallback` -> `useCallback` + `worklet` directive;
- removed unnecessary `@ts-expect-error` directives;

### e2e
- updated `AwareScrollViewKeyboardClosed` screenshot (it should have
scroll back when keyboard gets hidden);

## 馃 How Has This Been Tested?

Tested manually on API 28 emulator + e2e tests;

## 馃摑 Checklist

- [x] CI successfully passed
  • Loading branch information
kirillzyusko committed Dec 4, 2023
1 parent 81c0b0d commit 64dc299
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 87 deletions.
32 changes: 9 additions & 23 deletions FabricExample/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1136,34 +1136,20 @@ PODS:
- React-Codegen
- React-RCTFabric
- ReactCommon/turbomodule/core
- RNReanimated (3.4.2):
- DoubleConversion
- FBLazyVector
- glog
- RNReanimated (3.6.1):
- hermes-engine
- RCT-Folly
- RCT-Folly (= 2021.07.22.00)
- RCTRequired
- RCTTypeSafety
- React-callinvoker
- React-Codegen
- React-Core
- React-Core/DevSupport
- React-Core/RCTWebSocket
- React-CoreModules
- React-cxxreact
- React-hermes
- React-jsi
- React-jsiexecutor
- React-jsinspector
- React-RCTActionSheet
- React-RCTAnimation
- React-RCTBlob
- React-debug
- React-Fabric
- React-graphics
- React-NativeModulesApple
- React-RCTFabric
- React-RCTImage
- React-RCTLinking
- React-RCTNetwork
- React-RCTSettings
- React-RCTText
- React-utils
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- Yoga
- RNScreens (3.24.0):
Expand Down Expand Up @@ -1443,7 +1429,7 @@ SPEC CHECKSUMS:
ReactCommon: 4b2bdcb50a3543e1c2b2849ad44533686610826d
RNCMaskedView: 949696f25ec596bfc697fc88e6f95cf0c79669b6
RNGestureHandler: 0f92ab218707c97810df75609886828ee1b7c5e3
RNReanimated: 5008fe999d57038a1c5c1163044854d453f41b98
RNReanimated: 6caa64a3a8f5c540cf1d519a79566b735baf2a27
RNScreens: e114ddbc9b91fbde662870c4256196ab0833314b
SocketRocket: f32cd54efbe0f095c4d7594881e52619cfe80b17
Yoga: 3efc43e0d48686ce2e8c60f99d4e6bd349aff981
Expand Down
2 changes: 1 addition & 1 deletion FabricExample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"react-native": "0.72.4",
"react-native-gesture-handler": "2.12.1",
"react-native-keyboard-controller": "link:../",
"react-native-reanimated": "3.4.2",
"react-native-reanimated": "3.6.1",
"react-native-safe-area-context": "4.7.1",
"react-native-screens": "3.24.0",
"react-native-toast-message": "^2.1.6"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC } from 'react';
import React, { FC, useCallback } from 'react';
import { ScrollViewProps, useWindowDimensions } from 'react-native';
import { FocusedInputLayoutChangedEvent, useReanimatedFocusedInput } from 'react-native-keyboard-controller';
import Reanimated, {
Expand All @@ -9,7 +9,6 @@ import Reanimated, {
useAnimatedScrollHandler,
useAnimatedStyle,
useSharedValue,
useWorkletCallback,
} from 'react-native-reanimated';
import { useSmoothKeyboardHandler } from './useSmoothKeyboardHandler';

Expand Down Expand Up @@ -84,7 +83,9 @@ const KeyboardAwareScrollView: FC<KeyboardAwareScrollViewProps> = ({
/**
* Function that will scroll a ScrollView as keyboard gets moving
*/
const maybeScroll = useWorkletCallback((e: number, animated: boolean = false) => {
const maybeScroll = useCallback((e: number, animated: boolean = false) => {
'worklet';

const visibleRect = height - keyboardHeight.value;
const point = (layout.value?.layout.absoluteY || 0) + (layout.value?.layout.height || 0);

Expand Down
8 changes: 4 additions & 4 deletions FabricExample/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5191,10 +5191,10 @@ react-native-gesture-handler@2.12.1:
version "0.0.0"
uid ""

react-native-reanimated@3.4.2:
version "3.4.2"
resolved "https://registry.yarnpkg.com/react-native-reanimated/-/react-native-reanimated-3.4.2.tgz#744154fead6d8d31d5bd9ac617d8c84d74a6f697"
integrity sha512-FbtG+f1PB005vDTJSv4zAnTK7nNXi+FjFgbAM5gOzIZDajfph2BFMSUstzIsN8T77+OKuugUBmcTqLnQ24EBVg==
react-native-reanimated@3.6.1:
version "3.6.1"
resolved "https://registry.yarnpkg.com/react-native-reanimated/-/react-native-reanimated-3.6.1.tgz#5add41efafac6d0befd9786e752e7f26dbe903b7"
integrity sha512-F4vG9Yf9PKmE3GaWtVGUpzj3SM6YY2cx1yRHCwiMd1uY7W0gU017LfcVUorboJnj0y5QZqEriEK1Usq2Y8YZqg==
dependencies:
"@babel/plugin-transform-object-assign" "^7.16.7"
"@babel/preset-typescript" "^7.16.7"
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 3 additions & 28 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -497,35 +497,10 @@ PODS:
- React-Core
- RNGestureHandler (2.12.1):
- React-Core
- RNReanimated (3.4.2):
- DoubleConversion
- FBLazyVector
- glog
- hermes-engine
- RCT-Folly
- RCTRequired
- RCTTypeSafety
- React-callinvoker
- RNReanimated (3.6.1):
- RCT-Folly (= 2021.07.22.00)
- React-Core
- React-Core/DevSupport
- React-Core/RCTWebSocket
- React-CoreModules
- React-cxxreact
- React-hermes
- React-jsi
- React-jsiexecutor
- React-jsinspector
- React-RCTActionSheet
- React-RCTAnimation
- React-RCTAppDelegate
- React-RCTBlob
- React-RCTImage
- React-RCTLinking
- React-RCTNetwork
- React-RCTSettings
- React-RCTText
- ReactCommon/turbomodule/core
- Yoga
- RNScreens (3.24.0):
- React-Core
- React-RCTImage
Expand Down Expand Up @@ -776,7 +751,7 @@ SPEC CHECKSUMS:
ReactCommon: 4b2bdcb50a3543e1c2b2849ad44533686610826d
RNCMaskedView: 949696f25ec596bfc697fc88e6f95cf0c79669b6
RNGestureHandler: c0d04458598fcb26052494ae23dda8f8f5162b13
RNReanimated: 726395a2fa2f04cea340274ba57a4e659bc0d9c1
RNReanimated: fdbaa9c964bbab7fac50c90862b6cc5f041679b9
RNScreens: b21dc57dfa2b710c30ec600786a3fc223b1b92e7
SocketRocket: f32cd54efbe0f095c4d7594881e52619cfe80b17
Yoga: 3efc43e0d48686ce2e8c60f99d4e6bd349aff981
Expand Down
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"react-native": "0.72.4",
"react-native-gesture-handler": "2.12.1",
"react-native-keyboard-controller": "link:../",
"react-native-reanimated": "3.4.2",
"react-native-reanimated": "3.6.1",
"react-native-safe-area-context": "^4.7.1",
"react-native-screens": "^3.24.0",
"react-native-toast-message": "^2.1.6"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC } from 'react';
import React, { FC, useCallback } from 'react';
import { ScrollViewProps, useWindowDimensions } from 'react-native';
import { FocusedInputLayoutChangedEvent, useReanimatedFocusedInput } from 'react-native-keyboard-controller';
import Reanimated, {
Expand All @@ -9,7 +9,6 @@ import Reanimated, {
useAnimatedScrollHandler,
useAnimatedStyle,
useSharedValue,
useWorkletCallback,
} from 'react-native-reanimated';
import { useSmoothKeyboardHandler } from './useSmoothKeyboardHandler';

Expand Down Expand Up @@ -84,7 +83,9 @@ const KeyboardAwareScrollView: FC<KeyboardAwareScrollViewProps> = ({
/**
* Function that will scroll a ScrollView as keyboard gets moving
*/
const maybeScroll = useWorkletCallback((e: number, animated: boolean = false) => {
const maybeScroll = useCallback((e: number, animated: boolean = false) => {
'worklet';

const visibleRect = height - keyboardHeight.value;
const point = (layout.value?.layout.absoluteY || 0) + (layout.value?.layout.height || 0);

Expand Down
8 changes: 4 additions & 4 deletions example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4940,10 +4940,10 @@ react-native-gesture-handler@2.12.1:
version "0.0.0"
uid ""

react-native-reanimated@3.4.2:
version "3.4.2"
resolved "https://registry.yarnpkg.com/react-native-reanimated/-/react-native-reanimated-3.4.2.tgz#744154fead6d8d31d5bd9ac617d8c84d74a6f697"
integrity sha512-FbtG+f1PB005vDTJSv4zAnTK7nNXi+FjFgbAM5gOzIZDajfph2BFMSUstzIsN8T77+OKuugUBmcTqLnQ24EBVg==
react-native-reanimated@3.6.1:
version "3.6.1"
resolved "https://registry.yarnpkg.com/react-native-reanimated/-/react-native-reanimated-3.6.1.tgz#5add41efafac6d0befd9786e752e7f26dbe903b7"
integrity sha512-F4vG9Yf9PKmE3GaWtVGUpzj3SM6YY2cx1yRHCwiMd1uY7W0gU017LfcVUorboJnj0y5QZqEriEK1Usq2Y8YZqg==
dependencies:
"@babel/plugin-transform-object-assign" "^7.16.7"
"@babel/preset-typescript" "^7.16.7"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"react": "18.2.0",
"react-native": "0.72.4",
"react-native-builder-bob": "^0.18.0",
"react-native-reanimated": "3.4.2",
"react-native-reanimated": "3.6.1",
"release-it": "^14.2.2",
"typescript": "^4.8.4"
},
Expand Down
1 change: 0 additions & 1 deletion src/animated.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ export const KeyboardProvider = ({
onFocusedInputLayoutChangedReanimated={inputHandler}
navigationBarTranslucent={navigationBarTranslucent}
statusBarTranslucent={statusBarTranslucent}
// @ts-expect-error https://github.com/software-mansion/react-native-reanimated/pull/4923
style={styles.container}
>
{children}
Expand Down
12 changes: 7 additions & 5 deletions src/components/KeyboardAvoidingView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Reanimated, {
useAnimatedStyle,
useDerivedValue,
useSharedValue,
useWorkletCallback,
} from 'react-native-reanimated';

import { useKeyboardAnimation } from './hooks';
Expand Down Expand Up @@ -68,18 +67,22 @@ const KeyboardAvoidingView = forwardRef<View, React.PropsWithChildren<Props>>(
const keyboard = useKeyboardAnimation();
const { height: screenHeight } = useWindowDimensions();

const relativeKeyboardHeight = useWorkletCallback(() => {
const relativeKeyboardHeight = useCallback(() => {
'worklet';

const keyboardY =
screenHeight - keyboard.heightWhenOpened.value - keyboardVerticalOffset;

return Math.max(frame.value.y + frame.value.height - keyboardY, 0);
}, [screenHeight, keyboardVerticalOffset]);

const onLayoutWorklet = useWorkletCallback((layout: LayoutRectangle) => {
const onLayoutWorklet = useCallback((layout: LayoutRectangle) => {
'worklet';

if (keyboard.isClosed.value) {
initialFrame.value = layout;
}
});
}, []);
const onLayout = useCallback<NonNullable<ViewProps['onLayout']>>(
(e) => {
runOnUI(onLayoutWorklet)(e.nativeEvent.layout);
Expand Down Expand Up @@ -134,7 +137,6 @@ const KeyboardAvoidingView = forwardRef<View, React.PropsWithChildren<Props>>(

return (
<Reanimated.View
// @ts-expect-error because `ref` from reanimated is not compatible with react-native
ref={ref}
onLayout={onLayout}
style={combinedStyles}
Expand Down
7 changes: 1 addition & 6 deletions src/components/KeyboardStickyView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ const KeyboardStickyView = forwardRef<
);

return (
<Reanimated.View
// @ts-expect-error because `ref` from reanimated is not compatible with react-native
ref={ref}
style={styles}
{...props}
>
<Reanimated.View ref={ref} style={styles} {...props}>
{children}
</Reanimated.View>
);
Expand Down
7 changes: 4 additions & 3 deletions src/replicas.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { useEffect, useMemo, useRef } from 'react';
import { useCallback, useEffect, useMemo, useRef } from 'react';
import { Animated, Easing, Keyboard, Platform } from 'react-native';
import {
runOnUI,
useAnimatedReaction,
useDerivedValue,
useSharedValue,
useWorkletCallback,
withSpring,
} from 'react-native-reanimated';

Expand Down Expand Up @@ -111,7 +110,9 @@ export const useReanimatedKeyboardAnimationReplica = () => {

const progress = useDerivedValue(() => height.value / heightEvent.value);

const handler = useWorkletCallback((_height: number) => {
const handler = useCallback((_height: number) => {
'worklet';

heightEvent.value = _height;
}, []);

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7769,10 +7769,10 @@ react-native-builder-bob@^0.18.0:
optionalDependencies:
jetifier "^2.0.0"

react-native-reanimated@3.4.2:
version "3.4.2"
resolved "https://registry.yarnpkg.com/react-native-reanimated/-/react-native-reanimated-3.4.2.tgz#744154fead6d8d31d5bd9ac617d8c84d74a6f697"
integrity sha512-FbtG+f1PB005vDTJSv4zAnTK7nNXi+FjFgbAM5gOzIZDajfph2BFMSUstzIsN8T77+OKuugUBmcTqLnQ24EBVg==
react-native-reanimated@3.6.1:
version "3.6.1"
resolved "https://registry.yarnpkg.com/react-native-reanimated/-/react-native-reanimated-3.6.1.tgz#5add41efafac6d0befd9786e752e7f26dbe903b7"
integrity sha512-F4vG9Yf9PKmE3GaWtVGUpzj3SM6YY2cx1yRHCwiMd1uY7W0gU017LfcVUorboJnj0y5QZqEriEK1Usq2Y8YZqg==
dependencies:
"@babel/plugin-transform-object-assign" "^7.16.7"
"@babel/preset-typescript" "^7.16.7"
Expand Down

0 comments on commit 64dc299

Please sign in to comment.