Soy un desarrollador principiante de React.
Estoy limpiando mis códigos.
Estoy tratando de deshacerme de la declaración if-else tanto como sea posible, pero no sé cómo manejar esta función.
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); } };
Realmente odio este código. ¿Me puede ayudar?
Puede encontrar el índice ( Array#findIndex
) y agregar uno.
const widths = [width.phone, width.tablet, width.smallDesktop, width.desktop], calc = width => setPerPage(widths.findIndex(w => width < w) + 1) || 6);