function App() {
const pagesTotal = 12 // any number
const [currentPage, setCurrentPage] = useState(1)
const isCurrent = page => ({background: page === currentPage && 'red'})
return (
<div style={{display: 'flex'}}>
<button onClick={() => setCurrentPage((prev) => prev - 1)} disabled={currentPage === 1}>PREV</button>
<button onClick={() => setCurrentPage(1)} style={isCurrent(1)}>1</button>
{currentPage > 4 && <div>...</div>}
{currentPage < 5 && <button style={isCurrent(2)} onClick={() => setCurrentPage(2)}>2</button>}
{currentPage < 5 && <button style={isCurrent(3)} onClick={() => setCurrentPage(3)}>3</button>}
{currentPage < 5 && <button style={isCurrent(4)} onClick={() => setCurrentPage(4)}>4</button>}
{currentPage < 5 && <button style={isCurrent(5)} onClick={() => setCurrentPage(5)}>5</button>}
{currentPage > 4 && currentPage < pagesTotal - 3 && <button style={isCurrent(currentPage - 1)} onClick={() => setCurrentPage(currentPage - 1)}>{currentPage - 1}</button>}
{currentPage > 4 && currentPage < pagesTotal - 4 && <button style={isCurrent(currentPage)} onClick={() => setCurrentPage(currentPage)}>{currentPage}</button>}
{currentPage > 4 && currentPage < pagesTotal - 4 && <button style={isCurrent(currentPage + 1)} onClick={() => setCurrentPage(currentPage + 1)}>{currentPage + 1}</button>}
{currentPage > pagesTotal - 5 && <button style={isCurrent(pagesTotal - 4)} onClick={() => setCurrentPage(pagesTotal - 4)}>{pagesTotal - 4}</button>}
{currentPage > pagesTotal - 5 && <button style={isCurrent(pagesTotal - 3)} onClick={() => setCurrentPage(pagesTotal - 3)}>{pagesTotal - 3}</button>}
{currentPage > pagesTotal - 4 && <button style={isCurrent(pagesTotal - 2)} onClick={() => setCurrentPage(pagesTotal - 2)}>{pagesTotal - 2}</button>}
{currentPage > pagesTotal - 4 && <button style={isCurrent(pagesTotal - 1)} onClick={() => setCurrentPage(pagesTotal - 1)}>{pagesTotal - 1}</button>}
{currentPage < pagesTotal - 3 && <div>...</div>}
<button style={isCurrent(pagesTotal)} onClick={() => setCurrentPage(pagesTotal)}>{pagesTotal}</button>
<button onClick={() => setCurrentPage((prev) => prev + 1)} disabled={currentPage === pagesTotal}>NEXT</button>
</div>
);
}
// ...прилегают к первой кнопке
const showAfterFirst = currentPage < 5;
// ...прилегают к последней кнопке
const showBeforeLast = currentPage > totalPages - 4;
!showAfterFirst
и !showBeforeLast
. А чтобы отобразить центральную группу кнопок, оба базовых условия должны быть ложными: !(showAfterFirst || showBeforeLast)
.