$ git show pull-refresh
Custom pull to refresh
Pull the list down to reveal a growing, rotating icon; release past the threshold to refresh.
Inspired by Google ChromeFrom William Candillon’s “Can it be done in React Native?” series. The code here is a simplified rewrite for Reanimated 4 and 3.
import { scheduleOnRN } from 'react-native-worklets';
const pull = useSharedValue(0), refreshing = useSharedValue(false);
const onScroll = useAnimatedScrollHandler({
onScroll: e => { pull.value = Math.max(0, -e.contentOffset.y); },
onEndDrag: () => {
if (pull.value > 80 && !refreshing.value) {
refreshing.value = true;
scheduleOnRN(onRefresh); // set refreshing.value = false when done
}
},
});
const icon = useAnimatedStyle(() => ({
opacity: interpolate(pull.value, [0, 80], [0, 1], Extrapolation.CLAMP),
transform: [
{ scale: interpolate(pull.value, [0, 80], [0.5, 1], Extrapolation.CLAMP) },
{ rotate: pull.value * 3 + 'deg' },
],
}));
// iOS bounce gives negative offsets. On Android, drive pull from a Gesture.Pan instead.