简体   繁体   中英

how to show loader in react . using hooks

I am using axios for communicate with server.I want to show loader when user request to server and hide the loader when request is complete

So i make a custom component to do this task .but my UI hang when I click multiple times on same button

const Loader = () => {
    const { loadingCount } = useLoadingState(),
        {showLoading, hideLoading} = useLoadingActions();

    useEffect(()=>{
        const self = this
        axios.interceptors.request.use(function (config) {
            showLoading();
            return config
        }, function (error) {
            return Promise.reject(error);
        });

        axios.interceptors.response.use(function (response) {
            // spinning hide
            // self.props.loading(false)
            hideLoading()
            return response;
        }, function (error) {
            hideLoading();
            return Promise.reject(error);
        });
    })


    return (
        <div>
            {loadingCount > 0 ?<div style={{position:"fixed",display:"flex",justifyContent:"center",alignItems:"center",width:'100%',height:'100%',zIndex:999999}}>
                {/*{loadingCount > 0 ? */}
                <Spin tip="Loading..." style={{zIndex:999999}}></Spin>
                {/*: null}*/}
            </div>: null}
        </div>





    );
};

Problem is on useeffect

when I comment out useEffect code it works perfectly .

NoTe : showloading and hideloading increase and decrease the loading count.

I think I have deallocate axios object the when component is unmount .???

Add empty array to sencod parameter to useEffect .
It works like componentDidMount() in functional component.

 const { useState, useEffect } = React; const Counter = () => { const [count, setCount] = useState(0) const [isLoaded, setIsLoaded] = useState(false); useEffect(() => { setTimeout(() => { setIsLoaded(true); }, 3000); }, []); // here return ( <div> { isLoaded && <div> <p>You clicked {count} times</p> <button onClick={() => setCount(count + 1)}> Click me </button> </div> } </div> ) } ReactDOM.render(<Counter />, document.getElementById('app'))
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.7.0-alpha.2/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.7.0-alpha.2/umd/react-dom.production.min.js"></script> <div id="app"></div>

i usualy use this code to show loading when request data is processing and hide when it's done

 const Loader = () => { const {data, setdata} = useState([]) useEffect(()=>{ axios.get('your host').then(res => { setdata(res.data); }).catch(err => { setdata(res.data); } }); return ( <div> {data.length > 0 ? <div style={{position:"fixed",display:"flex",justifyContent:"center",alignItems:"center",width:'100%',height:'100%',zIndex:999999}}> </div> : <Spin tip="Loading..." style= {{zIndex:999999}}> </Spin> </div> ); };
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

First I created Loading components in shared folder. I am using Daisy UI, that's why you have to first install tailwind & daisy otherwise it will not work. My Loading Code:

import React from 'react';

const Loading = () => {
    return (
        <div className="flex items-center justify-center ">
        <div className="w-16 h-16 border-b-2 border-gray-900 rounded-full animate-spin"></div>
    </div>
    );
};

export default Loading;

Then I am using this Loading component in my Allproduct component. For viewing Loading i created Reload useState.You will see below in my code, that will help my loader show when fetching time is very long.

import React, { useEffect, useState } from 'react';
import Loading from '../Shared/Loading';
import AllProduct from './AllProduct';

const AllProducts = () => {
    const [products, setProduct]=useState([])
    const [Reload, setReload] = useState(true);

    useEffect(()=>{
        fetch('https://stormy-hamlet-97462.herokuapp.com/products/')
        .then(res=>res.json())
        .then(data=>{setProduct(data)
            setReload(false)})

    },[])

    if(Reload){
        return <Loading></Loading>
    }
    return (
        <div>
           <h4 className='text-4xl text-primary text-center sm:w-full px-32 mx-5 
           lg:my-12 '>All Products</h4>
             <div className='grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-5'>
               {
                   products.map(product=><AllProduct key={product._id} product={product} ></AllProduct>)
               }

           </div>
            
        </div>
    );
};

export default AllProducts;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM