Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show loading spinner with minimum time

Tags:

reactjs

axios

I implemented a page loader during axios request but it shows for a very short time. How to show it for a minimum 2 seconds or with fancy fade in react? Is it possible?

const Loader = () => (
    <div class="divLoader">
      Loading..
    </div>
 );

class dashboard extends Component {
    constructor(props) {
        super(props);
        this.state = {
            loading: true,
            data: [],
        }
    }

    componentDidMount() {
            axios.get("http://localhost:8080/user/dashboard")
            .then((res) => {
                const data = res.data;
                this.setState({ data, loading: false })

            }, (error) => {
                this.handleLogout();
                console.log("dasboard ERR");
            });


    }


    render() {
        return (
            <div className="container-xxl">
                 {this.state.loading ? <Loader /> : <Content />}
             </div>
    )};

}

like image 347
Kamikaze Avatar asked Oct 17 '25 21:10

Kamikaze


1 Answers

You have to design what you're trying to achieve by considering two things.

If your API response time is very low then keep the loader for an extra few times by applying a setTimeOut. But here you've got another thing to keep in mind. What if your API response time takes a moderate time which is enough as the lifetime of a loader to show? In this case, you may not keep your loader for extra time as that will make the visitors bored. So, what you have to do is keeping a timer. Say, x(in milisecond) is the least time you want to show the loader. If elapsed time (or the time API is taking to respond i.e., the loader is live for this time) is greater than x then you're done. If not then setTimeOut for (x - elapsed time).

like image 90
Mateen Avatar answered Oct 19 '25 13:10

Mateen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!