React如何命令式调用自定义的Antd-Modal组件

目录结构

├─RiskModal
│      index.js	//将riskModal与show等方法结合起来并导出
│      riskModal.js	//自定义Modal
│      riskModal.less
│      show.js	//命令时调用

riskModal.js

import { Modal } from 'antd-mobile';

const RiskModal = ({
                     visible,
                     onClose,
						...
                   }) => {
	...
  return (
    <Modal visible={visible} onClose={onClose}>
	...自定义样式
	</Modal>)
}

以前多数情况下,都是在父函数式组件中作为组件去调用,每次都需要定义visible状态并传入。

show.js

import { renderImperatively } from '@/utils/render-imperatively';

export const show = (props) => {
  return renderImperatively(
    <RiskModal
      {...props}
    />,
  );
};
render-imperatively.js 命令式渲染组件
import React, { useEffect, useImperativeHandle, useRef, useState } from 'react';
import { renderToBody } from '@/utils/renderToBody';
import { isFunction } from 'lodash';

export function renderImperatively(element) {
  //Wrapper组件单独维护一个visible状态,React.forwardRef主要是用来暴露close方法给父组件调用
  const Wrapper = React.forwardRef((_, ref) => {
    const [visible, setVisible] = useState(false);
    const closedRef = useRef(false);
    //初始化组件时修改展示状态
    useEffect(() => {
      if (!closedRef.current) {
        setVisible(true);
      } else {
        afterClose();
      }
    }, []);

    function onClose() {
      closedRef.current = true;
      setVisible(false);
      if (isFunction(element?.props?.onClose))
        element.props.onClose();
    }

    function afterClose() {
      unmount();
      if (isFunction(element?.props?.onClose))
        element.props.onClose();
    }

    useImperativeHandle(ref, () => ({
      close: onClose,
    }));

    return React.cloneElement(element, {
      ...element.props,
      visible,
      onClose,
      afterClose,
    });
  });
  const wrapperRef = React.createRef();
  //将新复制的组件挂载到body上,重点其实是这个
  const unmount = renderToBody(<Wrapper ref={wrapperRef} />);

  function close() {
    if (isFunction(wrapperRef?.current?.close))
      wrapperRef.current.close();
  }

  return {
    close,
  };
}
renderToBody.js 将组件挂载到body
import ReactDOM from 'react-dom';

export const renderToBody = (element) => {
  const container = document.createElement('div');
  document.body.appendChild(container);

  function unmount() {
    const unmountResult = ReactDOM.unmountComponentAtNode(container);
    if (unmountResult && container.parentNode) {
      container.parentNode.removeChild(container);
    }
  }

  ReactDOM.render(element, container);
  return unmount;
};

index.js

import RiskModal from './riskModal';
import { show } from './show';
import { attachPropertiesToComponent } from '@/utils/attach-properties-to-component';

export default attachPropertiesToComponent(RiskModal, {
  show,
});
attach-properties-to-component.js 给组件增加属性
export const attachPropertiesToComponent = (component, properties) => {
  const ret = component;
  for (const key in properties) {
    if (properties.hasOwnProperty(key)) {
      ret[key] = properties[key];
    }
  }
  return ret;
};

命令式调用

import RiskModal from '@/components/RiskModal';

RiskModal.show({
      type,
    });
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值