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

Compose refs helper #57

Closed
souporserious opened this issue Feb 16, 2018 · 5 comments
Closed

Compose refs helper #57

souporserious opened this issue Feb 16, 2018 · 5 comments

Comments

@souporserious
Copy link
Collaborator

souporserious commented Feb 16, 2018

I was curious if you had any ideas about some type of utility to help with composing multiple ref functions that could possibly be added to this library? I'm thinking it would be similar to the composeEvents function we talked about in #32.

I'm wondering if there is any way to memoize all the function calls so we can avoid this caveat when composing multiple refs? It would be really cool if this could be baked into composeEvents as well.

This is my idea:

function memoize(fn) {
  memoize.cache = {}
  return function() {
    var key = JSON.stringify(arguments)
    if (memoize.cache[key]) {
      return memoize.cache[key]
    } else {
      var val = fn.apply(this, arguments)
      memoize.cache[key] = val
      return val
    }
  }
}

function memoizeRefs(...fns) {
  return node => {
    fns.forEach(fn => memoize(fn))
  }
}
@streamich
Copy link

Maybe React.createRef() could help?

@TrySound
Copy link
Collaborator

@streamich createRef is dead simple and not composable.

@TrySound
Copy link
Collaborator

reactjs/rfcs#30 (comment)

@renatorib
Copy link
Owner

renatorib commented Apr 11, 2018

I think composeEvents (#71) can handle multiple refs as well.

const handleRef1 = (ref) => console.log(1, ref)
const handleRef2 = (ref) => console.log(2, ref)

<div {...composeEvents({ ref: handleRef1 }, { ref: handleRef2 })}>
  Ref div
</div>

If someone wants to contribute with this caveat solution say here that I reopen this issue. ✌️

@souporserious
Copy link
Collaborator Author

Nice! Looks like it's working great here 🤘. Wonder if composeProps might be a better name since it can apply to more than events, thoughts? 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants