formsy-react icon indicating copy to clipboard operation
formsy-react copied to clipboard

Warnings about refs when using functional components

Open ghost opened this issue 6 years ago • 5 comments

I made a stateless functional component and exported it with withFormsy(Component). It works, but React prints warnings in the console:

"Stateless function components cannot be given refs. Attempts to access this ref will fail. Check the render method of Formsy(Component)"

Is there anything to do about this? I am not using refs in my component. It's me that doing something wrong or I can ignore it?

ghost avatar Sep 26 '18 11:09 ghost

Yeah, it should be safe to use. withFormsy assigns a functional ref to the wrapped element which is equal to the innerRef prop you pass to it or a noop function if you don't pass anything.

We should definitely remove this default noop function, especially since the truthyness of the innerRef prop is checked before it is passed on to the wrapped component.

If you want to get rid of the warnings, just set the innerRef prop to null or false when using your component.

MilosRasic avatar Sep 26 '18 11:09 MilosRasic

@MilosRasic damn that was fast. Thanks, it works.

ghost avatar Sep 26 '18 12:09 ghost

I'm glad we have a work-around, but is this something we can fix in Formsy? Is there a way to check before passing down ref?

rkuykendall avatar Feb 22 '20 17:02 rkuykendall

@rkuykendall do we want a PR to fix this? It seems to be easily achievable just removing the default noop function form the HOC

almarto avatar Feb 10 '21 09:02 almarto

@almarto Yeah a PR would be appreciated. I don't think I understand the issue but it sounds like a simple fix.

rkuykendall avatar Feb 20 '21 21:02 rkuykendall