React.forwardRef 获取子组件的dom

 

 

const FancyButton = React.forwardRef((props, ref) => (
  <button ref={ref} className="FancyButton">
    {props.children}
  </button>
));

function LoadingButton() {
  const ref = React.createRef();

  useEffect(() => {
    console.log(ref)
  }, [])
  return (
    <div className="m-test-wrap">
      <FancyButton ref={ref}>Click me!</FancyButton>
    </div>
  );
}

posted @ 2021-12-23 16:07  徐同保  阅读(0)  评论(0编辑  收藏  举报  来源