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.

TypeScript compliant Ref handling for animating the movement of arrows

See original GitHub issue

In trying to achieve animation of arrows on div scrolling, I ran into a couple of issues. I achieved this:

PYPHnYu5DH

But through some scullduggery.

I extended the ArcherContainerProps interface to contain:

  ref?: any

  forceUpdate?: () => void

So that I could grab the reference to the archer container and also forceUpdate the component (not sure why I couldn’t access these generic react props?)

Then I attached the event listeners to my divs and on scroll I forceUpdate the component.

export default class Space extends Component<Props, State> {
  state = {
    currentSelectionIdx: 0,
  }
  baseSpaceRef = React.createRef<HTMLDivElement>()
  linkSpaceRef = React.createRef<HTMLDivElement>()
  archerContainerRef = React.createRef<ArcherContainerProps>()

  componentDidMount = () => {
    this.baseSpaceRef.current &&
      this.baseSpaceRef.current.addEventListener('scroll', this.isScrolling)
    this.linkSpaceRef.current &&
      this.linkSpaceRef.current.addEventListener('scroll', this.isScrolling)
  }

  componentWillUnmount = () => {
    this.baseSpaceRef.current &&
      this.baseSpaceRef.current.removeEventListener('scroll', this.isScrolling)
    this.linkSpaceRef.current &&
      this.linkSpaceRef.current.removeEventListener('scroll', this.isScrolling)
  }

  isScrolling = () => {
    this.archerContainerRef!.current!.forceUpdate()
  }

  render() {
    return (
      <div>
        <ArcherContainer ref={this.archerContainerRef}>
          <div className="space">
            <div className="baseSpace" ref={this.baseSpaceRef}>
              {this.baseCards()}
            </div>
            <div className="linkedSpace" ref={this.linkSpaceRef}>
              {this.linkedCards()}
            </div>
          </div>
        </ArcherContainer>
      </div>
    )
  }
}

It feels hacky though.

I was wondering if you could supply the typescript compliant functionality? It may not need a new feature, just I haven’t found it yet.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dimapxcommented, Oct 18, 2019

@pierpo Thanks, works like a charm! Really appreciate the quick response 💯

0reactions
Rambatinocommented, Nov 25, 2019

@pierpo seems to!

Thanks!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Use Framer Motion With React, TypeScript and Styled ...
To prepare the animation we have to use the animate property. It should be added to the motion component (in this case it...
Read more >
How can I animate a react.js component onclick and detect the ...
I want to have a react component flip over when a user clicks on the DOM element. I see some documentation about their...
Read more >
Total Guide To Dynamic Angular Animations That Can Be ...
Angular animations are usually implemented using a declarative approach. We reference animations in a component's template using ...
Read more >
Blog | Remotion | Make videos programmatically in React
This package offers utilities for animating and manipulating SVG paths! With 9 pure, type-safe functions, we cover many common needs while ...
Read more >
react-beautiful-dnd-next - npm
This is to maximise performance by allowing the GPU to handle the movement. The CSS animation curve has been designed to communicate getting...
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