dimanche 7 novembre 2021

How can I clean up this if-else function? (Javascript ES6)

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 (100 < responsiveWidth.phone) {
            setPerPage(1);
        } else if (100 < responsiveWidth.tablet) {
            setPerPage(2);
        } else if (100 < responsiveWidth.smallDesktop) {
            setPerPage(3);
        } else if (100 < responsiveWidth.desktop) {
            setPerPage(4);
        } else {
            setPerPage(6);
        }
    };

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

Aucun commentaire:

Enregistrer un commentaire