Skip to content

Conversation

@tjzel
Copy link
Collaborator

@tjzel tjzel commented Mar 27, 2024

Summary

It's bad for React, and we can handle creating an unused shared value from time to time.

Test plan

:shipit:

Testing snippet
import { StyleSheet, View } from 'react-native';

import Animated, {
  useSharedValue,
  useScrollViewOffset,
  useAnimatedRef,
  useAnimatedStyle,
} from 'react-native-reanimated';

import React from 'react';

export default function EmptyExample() {
  const scrollViewRefA = useAnimatedRef<Animated.ScrollView>();
  const scrollViewRefB = useAnimatedRef<Animated.ScrollView>();
  const myOffset = useSharedValue(0);
  const hookOffset = useScrollViewOffset(scrollViewRefA);
  const hookOffsetFromMyOffset = useScrollViewOffset(scrollViewRefB, myOffset);

  const animatedStyleA = useAnimatedStyle(() => ({
    borderRadius: 100 * Math.abs(Math.sin(hookOffset.value / 400)),
  }));

  const animatedStyleB = useAnimatedStyle(() => ({
    borderRadius: 100 * Math.abs(Math.sin(hookOffsetFromMyOffset.value / 400)),
  }));

  return (
    <View style={styles.container}>
      <Animated.ScrollView
        ref={scrollViewRefA}
        horizontal={true}
        style={styles.scrollView}>
        {Array.from({ length: 100 }).map((_, index) => (
          <Animated.View key={index} style={[styles.box, animatedStyleA]} />
        ))}
      </Animated.ScrollView>
      <Animated.ScrollView
        ref={scrollViewRefB}
        horizontal={true}
        style={styles.scrollView}>
        {Array.from({ length: 100 }).map((_, index) => (
          <Animated.View key={index} style={[styles.box, animatedStyleB]} />
        ))}
      </Animated.ScrollView>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  box: {
    width: 200,
    height: 200,
    backgroundColor: 'blue',
    margin: 30,
  },
  scrollView: {
    maxWidth: 800,
    maxHeight: 300,
  },
});

@tjzel tjzel requested review from piaskowyk and szydlovsky March 27, 2024 11:58
Copy link
Contributor

@szydlovsky szydlovsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@tjzel tjzel added this pull request to the merge queue Mar 27, 2024
Merged via the queue into main with commit 75f9398 Mar 27, 2024
@tjzel tjzel deleted the @tjzel/useScrollViewOffset/no-conditional-hook branch March 27, 2024 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants