How can I clean up this if-else function?


I’m a beginner React developer.

I am cleaning up my codes.

I’m trying to get rid of the if-else statement as much as possible, but I don’t know how to handle this function.

    const calc = () => {
        if (width < Number(width.phone)) {
            setPerPage(1);
        } else if (width < Number(width.tablet)) {
            setPerPage(2);
        } else if (width < Number(width.smallDesktop)) {
            setPerPage(3);
        } else if (width < Number(width.desktop)) {
            setPerPage(4);
        } else {
            setPerPage(6);
        }
    };

I really hate this code.
Can you help me out?

Source: JavaSript – Stack Overflow

November 7, 2021
Category : News
Tags: code-cleanup | if-statement | javascript | reactjs | refactoring

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.