Creating sharedValue inside an hook
See original GitHub issueDescription
I’m creating an Animated list so that I can do
type ItemType = {
id: string
color: string
}
const Screen > = function ({}) {
const [items, setItems] = useState<ItemType[]>([])
...
return (
<DraggableList
data={items}
renderItem={_renderItem}
/>
)
}
In DraggableList
I want to maintain an up-to-date Map containing shared values like offset
.
What I would do normally is using useEffect
inside DraggableList
so that I have something like
const myMap = useRef(new Map()).current
useEffect(() => {
data.map((x) => {
if(!myMap.has(x.id))
myMap.set(x.id, { offset: useSharedValue(0)})
})
}, [data])
and some logic to handle delete / add of new items.
However, as expected, I cannot use useSharedValue
inside useEffect
.
How can I create sharedValues like I could do in V1 with new Animated.Value ?
Package versions
- React: 16.13.1
- React Native: 0.63.2
- React Native Reanimated: 2.0.0-alpha.7
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
useSharedValue | React Native Reanimated - Software Mansion
Use this hook to create a reference to a JavaScript value that can be shared with worklets. Shared Values serve a similar purpose...
Read more >Building Your Own Hooks - React
Building your own Hooks lets you extract component logic into reusable functions. ... (such as setting up a subscription and remembering the current...
Read more >How to use a custom-hook that returns a value, inside another ...
getUserInfro method, and then gets part of the returned object and sets a state variable with it. I also have another Hook called...
Read more >Hacking React Hooks: Shared Global State
Larry, Curly, Moe, and Curly Jr all use the custom Hook useCounter() . They all display the value of count . And they...
Read more >React Hooks cheat sheet: Best practices with examples
To help demonstrate how to solve common React Hooks questions, ... an initial state value, state could also be initialized from a function, ......
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
Ok I think I got it 😃 Thanks !
For reference:
Use context for this.
So you’ll need to wrap the list with a context, and the user should call
useDraaggableListItem()
where youuseContext
to grab all the data you need and do whatever logic you need.It can be either in the user’s component or in your wrapper around the list item you render (where you handle measurements and onPress).