<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: VladymyrPylypchatin</title>
    <description>The latest articles on DEV Community by VladymyrPylypchatin (@vladymyrpylypchatin).</description>
    <link>https://dev.to/vladymyrpylypchatin</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F292004%2F2b65a4b6-1de5-407f-928b-8d298d37481b.png</url>
      <title>DEV Community: VladymyrPylypchatin</title>
      <link>https://dev.to/vladymyrpylypchatin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vladymyrpylypchatin"/>
    <language>en</language>
    <item>
      <title>Simple Pagination in Next.js using react-paginate</title>
      <dc:creator>VladymyrPylypchatin</dc:creator>
      <pubDate>Sun, 29 Mar 2020 13:03:59 +0000</pubDate>
      <link>https://dev.to/vladymyrpylypchatin/simple-pagination-in-next-js-using-react-paginate-1d5e</link>
      <guid>https://dev.to/vladymyrpylypchatin/simple-pagination-in-next-js-using-react-paginate-1d5e</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2600%2F1%2A1-VC8YS5g1c82cjBwxbw1Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2600%2F1%2A1-VC8YS5g1c82cjBwxbw1Q.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The development of the Pagination in Next.js project can be tricky. A common way of building pagination like in CRA won’t be the best option in Next.js. It won’t be SEO Friendly.&lt;/p&gt;

&lt;p&gt;I will show you how to build a List of Posts with SEO Friendly Pagination in a Next.js project. For pagination UI I am going to use package react-paginate. Data for testing I am going to fetch from API of &lt;a href="https://gorest.co.in/" rel="noopener noreferrer"&gt;this service&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The resulting project looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1200%2F1%2AhQO3gminDgxaPgUN865Y5Q.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1200%2F1%2AhQO3gminDgxaPgUN865Y5Q.gif"&gt;&lt;/a&gt;&lt;br&gt;
&lt;span&gt;Pagination Demo in Next.js Project&lt;/span&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Prerequisites to Backend API
&lt;/h3&gt;

&lt;p&gt;To develop pagination in Next.js you need to have your Backend API prepared. Your Backend API response should provide a total count of the items, current page, and items of the page. Ideally, also you need to have a total page count.&lt;/p&gt;

&lt;p&gt;Example of the response from the Backend API with pagination.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Also, your backend needs to support fetching items of a certain page. For example &lt;code&gt;https://api.yourapp.com/posts?page=3&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Project Setup
&lt;/h3&gt;

&lt;p&gt;In the below example I use a next.js project created using &lt;code&gt;create-next-app&lt;/code&gt;. I installed package &lt;code&gt;axios&lt;/code&gt; for fetching API and package &lt;code&gt;react-paginate&lt;/code&gt; for pagination UI.&lt;/p&gt;

&lt;p&gt;You can install these package with this command&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm i axios react-paginate&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;All code of the project contains in &lt;code&gt;pages/index.js&lt;/code&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Let’s go through each piece of the code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fetching Posts
&lt;/h3&gt;

&lt;p&gt;We start with fetching posts via our API. We fetch them in a &lt;code&gt;getIntialProps&lt;/code&gt; hook that fires on the server-side on the first run and further navigations. It populates page during prerendering. Fetching data in this hook will make our pages SEO friendly.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Rendering Posts and Pagination
&lt;/h3&gt;

&lt;p&gt;Depending on the loading state we render either the text &lt;code&gt;Loading…&lt;/code&gt; or list of posts.&lt;/p&gt;

&lt;p&gt;Below I use component ReactPaginate from the package &lt;code&gt;react-paginate&lt;/code&gt;. We can configure what class names to set for pagination elements via props. Styles for the project are defined globally in styles.scss.&lt;/p&gt;

&lt;p&gt;Pages count and initial page index is also set via props. &lt;code&gt;onPageChange&lt;/code&gt; receives a pagination handler function which fires every time the user selects a new page.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Page Select Handler
&lt;/h3&gt;

&lt;p&gt;When a user selects a page, the &lt;code&gt;pagginationHandler&lt;/code&gt;function fires with the selected page index as the argument.&lt;/p&gt;

&lt;p&gt;We fetch data inside of the &lt;code&gt;getIntialProps&lt;/code&gt;. And &lt;code&gt;getIntialProps&lt;/code&gt;hook fires during page change and reads query param &lt;code&gt;page&lt;/code&gt;to fetch a certain data from Backend API.&lt;/p&gt;

&lt;p&gt;So we need to change the route with a new query param &lt;code&gt;page&lt;/code&gt;which will contain a page index to trigger posts fetch and component re-render.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Loading Indicator
&lt;/h3&gt;

&lt;p&gt;To improve user experience I added a loading indicator.&lt;/p&gt;

&lt;p&gt;Our posts fetching happens during the page change, we need to rely on router events to switch loading state. I created the state and 2 functions to switch it.&lt;/p&gt;

&lt;p&gt;Then after the component is mounted I set handlers on events &lt;code&gt;routeChangeStart&lt;/code&gt; and &lt;code&gt;routeChangeComplete&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When the component is unmounted I remove handlers from these events to avoid memory leaks.&lt;/p&gt;

&lt;p&gt;That's it. I hope this example helped you to understand how to build pagination in your Next.js project.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Project code
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/VladymyrPylypchatin/nextjs-pagination" rel="noopener noreferrer"&gt;https://github.com/VladymyrPylypchatin/nextjs-pagination&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Thanks for reading! :)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;I am Vova Pilipchatin, a &lt;a href="https://vpilip.com/about/" rel="noopener noreferrer"&gt;freelance Software Engineer and Web Developer.&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;If you are enjoyed this article, please follow me on &lt;a href="https://twitter.com/VPilipchatin" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;There I share what I learn about how to launching SaaS projects and building freelance business :)&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;This post was originally published on my &lt;a href="https://vpilip.com/how-build-simple-pagination-in-nextjs/" rel="noopener noreferrer"&gt;blog&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>The simple way to use Scoped and Global SCSS in Next.js
</title>
      <dc:creator>VladymyrPylypchatin</dc:creator>
      <pubDate>Fri, 13 Dec 2019 19:43:18 +0000</pubDate>
      <link>https://dev.to/vladymyrpylypchatin/the-simple-way-to-use-scoped-and-global-scss-in-next-js-3epa</link>
      <guid>https://dev.to/vladymyrpylypchatin/the-simple-way-to-use-scoped-and-global-scss-in-next-js-3epa</guid>
      <description>&lt;p&gt;When I started working with next.js I had difficulties with styling components. Although it has a default way of styling called CSS in JS, I preferer Create React App approach. When you can write SASS/SCSS styles in separate files and scope it to the file or make it global.&lt;/p&gt;

&lt;p&gt;The main problem was that the default next-sass package allowed only to use ether scoped styles or global. There wasn’t a simple way how to use it them both.&lt;br&gt;
So after some research and development, I figure out how to make it work with both of them. So I want to share with you how to make it, so you can save some of your time.&lt;/p&gt;
&lt;h2&gt;
  
  
  Warning!
&lt;/h2&gt;

&lt;p&gt;This method doesn't work with the latest next.js updates.I recommend using the approach in this comment &lt;a href="https://dev.to/vladymyrpylypchatin/comment/m7fg"&gt;https://dev.to/vladymyrpylypchatin/comment/m7fg&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  High-Level Overview
&lt;/h2&gt;

&lt;p&gt;First of all, we will need to install next-sass and node-sass packages in our project. Then we will need to write the custom configuration for webpack in the next.config.js to make these packages working as we want.&lt;br&gt;
After these steps, you will be able to just use import styles from &lt;code&gt;component.scss&lt;/code&gt; to make scoped to component styles. Or import &lt;code&gt;filname.global.scss&lt;/code&gt; to use global styles into a component.&lt;br&gt;
Let’s take a look at the details.&lt;/p&gt;
&lt;h2&gt;
  
  
  Install dependencies
&lt;/h2&gt;

&lt;p&gt;Run these commands to install packages @zeit/next-sass and node-sass.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save @zeit/next-sass node-sass
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
yarn add @zeit/next-sass node-sass
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Setup next.js configuration
&lt;/h2&gt;

&lt;p&gt;First, you need to create a file &lt;code&gt;next.config.js&lt;/code&gt; It is a predefined name so this file should have exactly this name otherwise it won’t work. &lt;code&gt;next.config.js&lt;/code&gt; is a file where we can configure our next.js application and tweak default webpack configs.&lt;br&gt;
Paste this code into your next.config.js, save it and restart your development server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const withSass = require('@zeit/next-sass');
module.exports = withSass({
  cssModules: true,
  cssLoaderOptions: {
    importLoaders: 2,
  },
  webpack: config =&amp;gt; {
    config.module.rules.forEach(rule =&amp;gt; {
      if (rule.test.toString().includes('.scss')) {
        rule.rules = rule.use.map(useRule =&amp;gt; {
          if (typeof useRule === 'string') {
            return { loader: useRule };
          }
          if (useRule.loader === 'css-loader') {
            return {
              oneOf: [
                {
                  test: new RegExp('.global.scss$'),
                  loader: useRule.loader,
                  options: {},
                },
                {
                  loader: useRule.loader,
                  options: { modules: true }
                },
              ],
            };
          }
          return useRule;
        });
        delete rule.use;
      }
    });
    return config;
  },
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  How to use
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Scoped styles
&lt;/h3&gt;

&lt;p&gt;To use scoped styles just create a file *.scssand import it to a react component. The imported variable will store an object with style’s class names mapped to its hash versions.&lt;br&gt;
Component.scss&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.someScssClass {
    height: 100px;
    backgrond: red;
    ...
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Component.jsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import styles from './Component.scss';
const Component = () =&amp;gt; {
    return (
        &amp;lt;div className={styles.someScssClass}&amp;gt;
        ...
    &amp;lt;/div&amp;gt;
    );
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Global styles
&lt;/h3&gt;

&lt;p&gt;To make global styles you need to create a file with this name convention *.global.scss and import it to the React component.&lt;/p&gt;

&lt;p&gt;styles.global.scss&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.globalClass{
    ...
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Component.jsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import '../{path}/styles.global.scss';
const Component = () =&amp;gt; {
    return (
        &amp;lt;div className="globalClass"}&amp;gt;
       ...
    &amp;lt;/div&amp;gt;
    );
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That’s it. Now you can easily use both global and local SCSS styles in your project. I hope this tutorial saved you some time.&lt;br&gt;
I am a freelance software engineer who loves to develop web and mobile applications, sass. Subscribe to get insights and lessons learned while building my freelance business and SasS projects :)&lt;/p&gt;

&lt;p&gt;JavaScript&lt;br&gt;
Nextjs&lt;br&gt;
React&lt;br&gt;
Web App Development&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>react</category>
      <category>scopedscss</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
