Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destroys ref prop passed to children #140

Open
natew opened this issue Apr 19, 2017 · 8 comments
Open

Destroys ref prop passed to children #140

natew opened this issue Apr 19, 2017 · 8 comments

Comments

@natew
Copy link

natew commented Apr 19, 2017

        <FlipMove>
            {[1, 2, 3, 4].map(x => <Test key={x} ref={console.log} />)}
          </FlipMove>

Ref wont callback here at all. But if you move it outside of map it works just fine!

@natew natew changed the title Destroys and ref props passed to children Destroys ref prop passed to children Apr 19, 2017
@joshwcomeau
Copy link
Owner

joshwcomeau commented Apr 20, 2017

Hm, you shouldn't be able to assign refs on children passed to FlipMove anyway - FlipMove clones the set of children you provide and overwrites the ref. That's how it works its magic; it needs access to the underlying DOM node.

What is it that you're trying to do exactly?

@natew
Copy link
Author

natew commented Apr 20, 2017 via email

@joshwcomeau
Copy link
Owner

Yeah, so as long as the ref isn't on the direct child, everything should be fine.

class YourForm extends Component {
  render() {
    return (
      <FlipMove>
        {[1,2,3,4].map(id => (
          <div>
            <input ref={...} />
          </div>
        )}
      </FlipMove>
    )
  }
}

You can (and maybe should) also extract the input into its own component.

class YourInput extends Component {
  render() {
    return <input ref={...} />;
  }
}

class YourForm extends Component {
  render() {
    return (
      <FlipMove>
        {[1,2,3,4].map(id => <YourInput key={id} />}
      </FlipMove>
    )
  }
}

@natew
Copy link
Author

natew commented Apr 21, 2017 via email

@joshwcomeau
Copy link
Owner

joshwcomeau commented Apr 23, 2017

Yeah, I can see how that'd be confusing. We could add it to the "Gotchas", although I don't know how much good that does; it isn't a place people typically check when experiencing an issue.

Maybe we can add a console warning if FlipMove detects a pre-existing ref on a supplied element.

Would be a great contribution if you or anyone else reading this wants to take it on :)

@natew
Copy link
Author

natew commented Apr 23, 2017 via email

@jahooma
Copy link

jahooma commented May 25, 2018

There's actually a simple way to pass the ref along so the original child can get it too.

After line 739 (https://github.com/joshwcomeau/react-flip-move/blob/master/src/FlipMove.js#L739), add the if statement:

if (typeof child.ref === 'function') {
  child.ref(node);
}

I believe this would work, but have not tested it. See also: facebook/react#8873

This solves a headache for a few users. In my case, I was trying to get the DOM refs for further calculations, like which children are currently visible in the scroll area.

Love your library by the way <3. We are using it profusely in our new social app, throne.live.

@joshwcomeau
Copy link
Owner

@jahooma interesting! That does seem like it'd work.

My time is stretched far too thin right now, but if you (or anyone else reading this) wanted to open a PR with that change, I'd happily merge it (just be sure to test that it works in the local storybook).

Also thanks for sharing throne.live, I always love seeing where Flip Move gets used :)

@joshwcomeau joshwcomeau reopened this May 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants