Passing a sharedValue as props to a component
See original GitHub issueDescription
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:
- Created 3 years ago
- Comments:16 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Instead of this
You should do this:
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
should be replaced with useDerivedValue
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
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 currentsize.value
.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.