Skip to content
This repository has been archived by the owner on Sep 10, 2022. It is now read-only.

Add documentation of refs and function components #25

Closed
vslinko opened this issue Oct 22, 2015 · 23 comments
Closed

Add documentation of refs and function components #25

vslinko opened this issue Oct 22, 2015 · 23 comments

Comments

@vslinko
Copy link

vslinko commented Oct 22, 2015

Because withProps using stateless components, connect attaching ref to child component, and react doesn't allow attach ref to stateless components.

@acdlite
Copy link
Owner

acdlite commented Oct 25, 2015

This is a problem with any function component, not just the ones provided by Recompose. One fix is add pure() in between connect() and withProps().

We should probably also document which helpers return a class component and which ones return a stateless component.

@acdlite acdlite added the docs label Oct 25, 2015
@vslinko
Copy link
Author

vslinko commented Oct 26, 2015

I found another solution: fresh version of redux doesn't use refs by default.

@vslinko
Copy link
Author

vslinko commented Oct 26, 2015

Should I close the issue?

@acdlite
Copy link
Owner

acdlite commented Oct 26, 2015

I'll keep it open as a reminder to add documentation about refs.

@acdlite acdlite changed the title Unable to use withProps under connect from redux Add documentation of refs and function components Oct 28, 2015
@emilong
Copy link

emilong commented May 22, 2016

I put together a jsfiddle that tests the output of the various HOCs here: https://jsfiddle.net/emilong/rcdovojw/3/

If this makes sense, I can take the output and add it to the documentation. One major assumption that would be good to get confirmation on is that Recompose.isClassComponent returning false means stateless component. Not sure if I'm getting the terminology and/or concepts right there.

@jeffbski
Copy link

jeffbski commented Jun 7, 2016

Having a nice way to add refs with recompose becomes even more important as React deprecates returning a component from render. Normally our tests use this component to find the DOM objects, but in the future we are supposed to use refs.

So we need a great way to get a ref to the hoc via recompose so we don't have to resort to manually created classes. I was thinking maybe a new helper like withRef ? Something that will ensure that regardless of what you have composed together with recompose that you will get a valid ref.

@emilong
Copy link

emilong commented Jun 8, 2016

@jeffbski does toClass() work for your case?

@jeffbski
Copy link

Possibly but I would like to see some examples of how that is done.

Looking at the API reference for toClass() doesn't give me any ideas about how I can create a ref.

Could someone add an example or point me to a test?

@acdlite
Copy link
Owner

acdlite commented Jun 10, 2016

Normally our tests use this component to find the DOM objects, but in the future we are supposed to use refs.

Why not use Enzyme? Refs are gross.

@acdlite
Copy link
Owner

acdlite commented Jun 10, 2016

See this issue for an explanation of the current problems with HOCs and refs facebook/react#6974

@jeffbski
Copy link

I haven't seen a need for Enzyme yet, my tests are really simple without it, though now this may be a reason to use it.

I don't like refs either, but they are the recommended approach for getting the root component back in React 16+ facebook/react#6397 since they are deprecating the return value of render().

Frankly if it were up to me, I'd rather have a way to optionally provide a callback to render and just get async notified with the component rather than using a ref.

@rnicholus
Copy link

Has anyone figured out how to actually use toClass, especially with refs? Documentation seems to be missing here.

@istarkov
Copy link
Contributor

istarkov commented May 16, 2017

What is your use case for refs with toClass?
I'm asking because you need toClass only if your component is a function, so why do you need an instance to it? If you need an instance of Dom element just write ref={registerRef} at top level Dom element of component.

@rnicholus
Copy link

If have contributed a ref function, but this.refs is always empty when I attempt to make use of this ref.

@rnicholus
Copy link

For example:

const enhance = compose(
  lifecycle({
    componentDidUpdate: function(prevProps) {
      console.log(this._myRef) // undefined
    }
  })
)

class Foo extends React.Component {
  render() {
    return <div ref={ component => this._myRef = component } />
  }
}

export default enhance(Foo)

@istarkov
Copy link
Contributor

Why not to register refs explicitly using function syntax in your case? ie

const MyComp = ({ registerChild }) => <div ref={registerChild}>Blabla</div>

Now you can get ref of MyComp using <MyComp registerChild={registerChild} />

@istarkov
Copy link
Contributor

Your case with lifecycle will not work at all, all is enhancer not the same component.
Your this points to lifecycle component instance not on yours. Please read about Higher-Order Components

@rnicholus
Copy link

so how can I get access to an element in this case? stateHandler as my ref function? or something else?

@istarkov
Copy link
Contributor

I don't know your case, I wrote before one of example how. There are many ways to do this.

@rnicholus
Copy link

i don't know what registerChild is in your example

@istarkov
Copy link
Contributor

What it can be based on React documentation?

@istarkov
Copy link
Contributor

It depends on case, in a lot of cases where I need refs I don't use recompose at all and use just React classes, as refs are mostly used at low level.

Sometimes I use them like,

compose(
  withHandlers(() => {
     let ref_;
     return {
        registerChild: () => (ref) => {
           ref_ = ref;
        },
        otherHandler: () => () => {
          // I can use ref here.
        }
     };
  })
)

Sometimes as like as above comment,
and sometimes for React Sortable for example I used it like

compose(
  bc => SortableContainer(bc, { withRef: true }), // eslint-disable-line
  toClass
);

so toClass will allow to get Dom element instance of any component

@rnicholus
Copy link

Thanks for the clarification. I'm thinking that recompose is not an ideal choice for this particular situation, as you mentioned earlier.

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

No branches or pull requests

6 participants