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.

Create animated refs dynamically

See original GitHub issue

Description

For using infinite lists (which not only add new rows on demand, but also delete old ones) it’s required to create animated refs dynamically. Now it’s only allowed to use useAnimatedRef which can’t be created on-demand.

Let’s say I added one page (rows are grouped by pages). I wrap first and last rows of page into Animated.View to measure it’s position inside onScroll.

That way I can understand the height of view , and track it’s position.

If page is shown in viewport of ScrollView - I can add another page.

Caveats

Motivation

High-performance scrolling, including infinite and cycled lists.

Code Example

Animated.createAnimatedRef()

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
BryanEnidcommented, Jul 10, 2022

I’m having the same issue. I need to define multiple animated “scrollviews” that are coming dynamically, but I need to have their refs in order to have access to their “scrollTo” method.

I tried something like this:

const x_refs_array = new Array(SCREENS.length).fill(useAnimatedRef());

const handleSubscreenXScroll = useAnimatedScrollHandler(() => {
    console.log(x_refs_array[0]()) //  -1
 })

// ...

return (
    <Animated.ScrollView
      // onScroll={handleScreenYScroll}
      horizontal
      ref={y_ref}
    >
      {dynamicsXScrollSections.map((content, index) => (
        <Animated.ScrollView
          // onScroll={handleSectionXScroll}
          ref={(ref) => (x_refs_array[index].current = ref)} // <----- here
        >
          {data}
        </Animated.ScrollView>
      ))}
    </Animated.ScrollView>
  );
1reaction
likerncommented, Jan 28, 2022

@jkadamczyk 👋🏻 Yes, you are right.

To extend a bit, you would like to have it because you want to store multiple refs, and you don’t know how many of them you want to store (perhaps how many comes from the API) and you want them to be mutable so you want to have a convenient way to re-assign them.

Yes. Not sure about reassignment (the original code is gone).

a callback option to assign ref anywhere like with <Component ref={ref => foo = ref} /> to be used in Animated Components

Yes.

something similar to React.createRef() but for Reanimated 2

Yes.

Ability to create animated refs dynamically (for example for every list element) Ability to create at any code (in callbacks or conditionally) without hooks restrictions. It can be implemented like React.createRef(), but not required.

The original idea was

  1. React Native allows to render <View /> (for every list element or conditionally)
  2. Wrap <View /> into <Animated.View />
  3. Use that parent <Animated.View /> to track <View />
  4. Track using ref.measure() and all worklet stuff on UI thread

This Feature Request helps Implement <FlatList /> purely on UI thread using <View /> as base component. And managing adding / deleting row ourselves. Implement viewabilityConfig and onViewableItemsChanged on UI thread https://reactnative.dev/docs/virtualizedlist#onviewableitemschanged.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to create animated view dynamically and keep other ...
I want to create a Barrage component for live videos: render() { return ( <View> <Video /> <BarrageLayer ref={r => this.
Read more >
Animate dynamically elements with React Hooks - GSAP
For what I understood, to use gsap to animate my elements, I need first to target them with ref and useRef(), I was...
Read more >
How to use React createRef - LogRocket Blog
Learn how to use the React.createRef() method and useRef Hook in React to simplify creating refs and interact with the HTML DOM.
Read more >
Animated - React Native
The core workflow for creating an animation is to create an Animated.Value , hook it up to one or more style attributes of...
Read more >
Create Refs dynamically in React - Medium
Under the hood use-dynamic-refs uses a singleton instance of Javascript Map . This gives you a store of key-value pairs of your refs...
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