Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

不支持获取ref,默认创建div无法删除,无法函数调用 #158

Open
Binote opened this issue Dec 24, 2021 · 0 comments
Open

Comments

@Binote
Copy link

Binote commented Dec 24, 2021

- export default (props: ViewerProps) => {
+ export default React.forwardRef((props: ViewerProps, ref) => {
  const defaultContainer = React.useRef(typeof document !== 'undefined' ? document.createElement('div') : null);
  const [ container, setContainer ] = React.useState(props.container);
  const [ init, setInit ] = React.useState(false);

  React.useEffect(() => {
+    if(!container){
       document.body.appendChild(defaultContainer.current);
+    }
  }, []);

  React.useEffect(() => {
    if (props.visible && !init) {
      setInit(true);
    }
  }, [props.visible, init]);

  React.useEffect(() => {
    if (props.container) {
      setContainer(props.container);
    } else {
      setContainer(defaultContainer.current);
    }
  }, [props.container]);

  if (!init) {
    return null;
  }
  return ReactDOM.createPortal((
    <ViewerCore
+      // 其他组件也需要用`React.forwardRef`来支持ref
+      ref={ref}
      {...props}
    />
  ), container);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant