Custom post pagination not working

Thanks to Milo’s comment I could find a solution.

In my home.php I have deleted the custom query and inserted:

<div class="news-content col-12 col-xl-8">
      <div class="row">
        <h1>News</h1>
      </div><!-- .row -->

      <div class="row">
          <?php

            if ( have_posts() ) : while ( have_posts() ) : the_post();

              if(has_post_thumbnail()) :?>

                <div class="news-wrapper col-12 col-xl-6">
                  <div class="news-date">
                    <?php the_time('j.m.Y'); ?>
                  </div><!-- .news-date -->
                  <div class="news-img">
                    <?php the_post_thumbnail(); ?>
                  </div><!-- .news-img -->
                  <a class="news-title" href="https://wordpress.stackexchange.com/questions/304812/<?php the_permalink() ?>">
                    <?php the_title(); ?>
                  </a><!-- .news-title -->
                  <div class="news-text">
                    <?php echo short_excerpt(25); ?>
                  </div>
                  <a href="https://wordpress.stackexchange.com/questions/304812/<?php the_permalink() ?>" class="btn">
                    czytaj dalej
                  </a><!-- .btn -->
                </div><!-- .news-wrapper -->

              <?php else :?>

                <div class="news-wrapper col-12 col-xl-6">
                  <div class="news-date">
                    <?php the_time('j.m.Y'); ?>
                  </div><!-- .news-date -->
                  <a class="news-title" href="https://wordpress.stackexchange.com/questions/304812/<?php the_permalink() ?>">
                    <?php the_title(); ?>
                  </a><!-- .news-title -->
                  <div class="news-text">
                    <?php echo short_news_content(70); ?>
                  </div>
                  <a href="https://wordpress.stackexchange.com/questions/304812/<?php the_permalink() ?>" class="btn">
                    czytaj dalej
                  </a><!-- .btn -->
                </div><!-- .news-wrapper -->

              <?php
                endif;
                endwhile;
                endif;
              ?>
      </div><!-- .row -->
      <div class="row">
        <?php the_posts_pagination( array( 'mid_size'  => 2 ) ); ?>
      </div>
    </div><!-- .news-content -->
  </div><!-- .container -->
</div><!-- .main-content -->

and in my functions.php file I have added:

add_action( 'pre_get_posts', 'custom_post_on_homepage' );

function custom_post_on_homepage( $query ) {

 if ( is_home() && $query->is_main_query() )
  {
    $query->set('post_type', 'news');
    $query->set( 'posts_per_page', '4' );
  }
 }

Hope this helps someone in the future.