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.

Simpler hook-only API without rendering a component?

See original GitHub issue

Issue: 🚀 Feature Proposal

Thank you for this amazing animation library!

I’ve been wondering if there’s any way for the animated component to be avoided at all by passing the ref directly to useSpring(s). As far as I understand, the animated component is used only so that there is access to the ref (to directly set styles) and otherwise props are just passed down, but maybe I’m missing something here.

Motivation

We’d like to create a hook that would contain useSpring that has access to the ref.

Example

What I’m proposing would be this:

const ref = useRef();
useSpring( { opacity: ..., target: ref } );

return <div ref={ ref } { ...otherProps } />;

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
aleclarsoncommented, Jun 5, 2020

FWIW, you can do something similar already (with v9):

import { Globals } from 'react-spring'

const { applyAnimatedValues } = Globals

const ref = useRef()
useSpring({
  opacity: visible ? 1 : 0,
  onChange(values) {
    applyAnimatedValues(ref.current, values)
  }
})

return <div ref={ref} />

You could make a custom hook to avoid duplication:

function useAnimatedRef(props) {
  const ref = useRef()
  useSpring({ 
    ...props, 
    onChange(values) {
      applyAnimatedValues(ref.current, values)
      if (props.onChange) props.onChange(values)
    }
  })
  return ref
}

const ref = useAnimatedRef({ opacity: visible ? 1 : 0 })
return <div ref={ref} />

edit: Oh wait, I forgot that applyAnimatedValues is not in Globals anymore. 😅 We should probably export it for each platform.

0reactions
joshuaelliscommented, Mar 18, 2021

closing due to inactivity. please use discussions or discord if you want more help or please consider creating a PR if you want to add a feature 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReactJS: how to call useEffect hook only once to fetch API data
So to execute the code only during initial render/component mount you can pass an empty array which means no dependencies specified when the ......
Read more >
Fetching Data and Updating State with React Hooks - Pluralsight
A previous guide covered how to fetch data from a REST API and how to re-render a React component with the results. The...
Read more >
Run useEffect Only Once - CSS-Tricks
useEffect will run when the component renders, which might be more ... is that if you're doing something like fetching data from an...
Read more >
Learn React Hooks – A Beginner's Guide - freeCodeCamp
Before React version 16.8 was introduced, functional components were ... and we only used them for rendering very simple components to the.
Read more >
Hooks API Reference - React
They let you use state and other React features without writing a class. ... Note that React may still need to render that...
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