digidem / react-dimensions

[Looking for maintainers]
http://lab.digital-democracy.org/react-dimensions/
450 stars 77 forks source link

ref error when wrapping stateless functional component #30

Open atomanyih opened 8 years ago

atomanyih commented 8 years ago

If you use Dimensions to wrap a stateless functional component it complains but still seems to work.

const SomebodyCaresAboutDimensions = ({containerWidth} => (
  <div>
    width: {containerWidth}
  </div>
));

export default Dimensions()(SomebodyCaresAboutDimensions);

will give

Warning: Stateless function components cannot be given refs (See ref "wrappedInstance" in SomebodyCaresAboutDimensions created by DimensionsHOC). Attempts to access this ref will fail.

gmaclennan commented 8 years ago

Thanks for this report. The ref is used for the instance method getWrappedInstance(), so I guess that method will not work for a stateless function component. Not sure what the solution is here, I'll leave this open and if anybody needs this method on a stateless function component they can look into it.

gmaclennan commented 8 years ago

Just to clarify: this error means that the getWrappedInstance() method will not work for stateless functional components, but everything else should continue to function as normal.

gmaclennan commented 8 years ago

We should do what redux does and make this opt-in: https://github.com/reactjs/react-redux/commit/2d3d0beade55477b3af65534ceb793db18b25705

gmaclennan commented 8 years ago

Would accept any pull-request that implements the same solution as the linked react-redux code.

oliviertassinari commented 8 years ago

Another approach would be to use a child function as react-virtualized/AutoSize is doing it (It's providing the same feature with a different API).

ml242 commented 7 years ago

Is it possible to merged this in?