styleguidist/react-styleguidist

Support for useImperativeHandle

Open

#1,426 创建于 2019年8月30日

在 GitHub 查看
 (0 评论) (3 反应) (0 负责人)TypeScript (1,416 fork)github user discovery
help wanted

仓库指标

Star
 (11,091 star)
PR 合并指标
 (PR 指标待抓取)

描述

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.

贡献者指南