question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Passing a sharedValue as props to a component

See original GitHub issue

Description

Basically I created a sharedValue and I want to use it in a child component. Is there a way to do that?

Code

const App = () => {
    const translateX = useSharedValue(0)

    const gestureHandler = useAnimatedGestureHandler({
        // ...
    })

    const animatedStyles = useAnimatedStyle(() => {
        return {
            transform: [{ translateX: translateX.value }]
        }
    })

    return (
        <View style={styles.container}>
            <PanGestureHandler onGestureEvent={gestureHandler}>
                <Animated.View style={[styles.box, animatedStyles]}>
                    <View style={styles.mainOption}>
                        <Text style={styles.mainContent}>Swipe to remove</Text>
                    </View>

                    <Action x={Math.abs(translateX.value)} />
                </Animated.View>
            </PanGestureHandler>
        </View >
    )
}


const Action = ({ x }) => {
    const size = useSharedValue(x < height ? x : x + (x - height))
    // ...
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

17reactions
terrysahaidakcommented, Nov 5, 2020

Instead of this

   const x = useSharedValue(0)

   useDerivedValue(() => {
      const formatedValue = Math.abs(props.x.value)
      x.value = formatedValue
   })

You should do this:

   const x = useDerivedValue(() => {
      const formatedValue = Math.abs(props.x.value)
      return formatedValue;
   })

useDerivedValue will be executed each time props.x.value has changed. It will result in the change of x.value. So no, x itself won’t be recreated, but the .value of it will be the result of useDerivedValue’s callback.

Moving to the next part.

if you need the shared value which depends on some other values, you again need to use useDerivedValue. So this

   const size = useSharedValue(
      x.value < height ?
         x.value :
         x.value + (x.value - height)
   )

   const translateX = useSharedValue(
      x.value < height ?
         0 :
         (x.value - height) / -2
   )

should be replaced with useDerivedValue

   const size = useDerivedValue(() =>
      x.value < height ?
         x.value :
         x.value + (x.value - height)
   )

   const translateX = useDerivedValue(() =>
      x.value < height ?
         0 :
         (x.value - height) / -2
   )

And the final part.

Interpolate - is just a function that returns a value. Only the time it’s executed. In order for it to return interpolated opacity - you need to use it inside the useAnimatedStyle like so

   const textStyles = useAnimatedStyle(() => {
     const opacity = interpolate(
       size.value,
       [height - 5, height + 5],
       [0, 1]
     );

      return {
         opacity: opacity
      }
   })

In this useAniamtedStyle, we access size.value so it subscribes for its changes. And now each time the size changes - we rerun the callback. So that interpolation function will be re-executed too. And will return a new value based on the current size.value.

7reactions
nitin-jotwani-fccommented, Aug 31, 2021

Update: Was able to see animation if I apply in useAnimatedStyle. But, if I want to access value to do something conditional inside child component, that I’m unable to do. size variable doesn’t update outside. But works fine when applied as an animation using useAnimatedStyle.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Passing Props to a Component - React Docs
React components use props to communicate with each other. Every parent component can pass some information to its child components by giving them...
Read more >
How passing props to component works in React
Master how to pass props (properties) to components in React with this useful beginner's guide, including demo code.
Read more >
Shared Values | React Native Reanimated - Software Mansion
Shared Values cannot be directly hooked as View's props. You should use useAnimatedStyle or useAnimatedProps where you can access Shared Values and return...
Read more >
How to use Props in React - Robin Wieruch
As you have seen, props enable you to pass values from one component to another component down the component tree. In the previous...
Read more >
How to Pass Data between React Components | Pluralsight
Use the variable this.props.dataFromParent to obtain the data passed from parent to child. 1class Child2 extends React.Component { 2 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found