React Hooks: use modal

Posted nyan-workflow-fc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了React Hooks: use modal相关的知识,希望对你有一定的参考价值。

useModal:

export const useModal = (initTitle: string, initContent: string | React.ReactElement) => 
    const [visible, setVisible] = useState(false);
    const [title, setTitle] = useState(initTitle);
    const [content, setContent] = useState(initContent);
    const CustomModal =  () => 
        return (
            <Modal
                visible=visible
                title=title
                closable=false
                footer=null
            >
                content
            </Modal>
        );
    
    const show = (content?: string | React.ReactElement) => 
        content && setContent(content);
        setVisible(true);
    ;
    const hide = (delay?: number) => 
        if (delay) 
            setTimeout(() => setVisible(false), delay);
         else 
            setVisible(false)
        
    ;
    return 
        show, hide, CustomModal, setTitle, setContent
    

use:

const hide, show, CustomModal = useModal(‘系统提示‘,  ‘正在初始化...‘);
 
render() 
    <div><CustomModal /></div>

 

以上是关于React Hooks: use modal的主要内容,如果未能解决你的问题,请参考以下文章

[React + GraphQL] Use useLazyQuery to manually execute a query with Apollo React Hooks

react自定义hooks-自动改变页面的title,Http请求hooks等..(持续更新)

使用 `react-apollo-hooks` 和 `useSubscription` 钩子

15 个很有用的自定义 React Hooks

使用@apollo/react-hooks,react 应用程序在刷新过期时严重崩溃

React Hooks 快速入门:从一个数据请求开始