styleguidist/react-styleguidist

Support for useImperativeHandle

Open

#1.426 geöffnet am 30. Aug. 2019

Auf GitHub ansehen
 (0 Kommentare) (3 Reaktionen) (0 zugewiesene Personen)TypeScript (1.416 Forks)github user discovery
help wanted

Repository-Metriken

Stars
 (11.091 Stars)
PR-Merge-Metriken
 (PR-Metriken ausstehend)

Beschreibung

The problem

useImperativeHandle allows components to expose some of their API similarly to instance class methods.

function FancyInput(props, ref) {
  const inputRef = useRef();
  useImperativeHandle(ref, () => ({
    focus: () => {
      inputRef.current.focus();
    }
  }));
  return <input ref={inputRef} ... />;
}
FancyInput = forwardRef(FancyInput);

allows calling fancyInputRef.current.focus() in other component.

Proposed solution

It would be great to treat functions returned from this hook as part of the external API and add them to the documentation

Alternative solutions

I guess, this needs to be done in the react-dogen but I'd like to keep track of this in this repo. We could also move the issue over.

Contributor Guide