美文网首页
自定义hook

自定义hook

作者: 一土二月鸟 | 来源:发表于2020-08-18 12:52 被阅读0次

    usage

    import React, { useEffect, useState } from "react";
    import ReactDom from "react-dom";
    
    const useProduct = (productName) => {
    
      const [proList, setProList] = useState([]);
    
      useEffect(() => {
    
        setTimeout(() => {
          setProList([1, 2, 3]);
        }, 1000);
        
      }, []);
    
      return proList;
      
    }
    
    const Index = () => {
    
      const proList = useProduct();
    
      return <>
        {proList.length}
      </>;
    
    }
    
    ReactDom.render(<Index />, document.getElementById("root"));
    

    相关文章

      网友评论

          本文标题:自定义hook

          本文链接:https://www.haomeiwen.com/subject/ficqjktx.html