1

I was about rounding up this activity and I started getting this error:

Module not found: Can't resolve './components/Post' in ./src/pages/index.js

I have tried all I could but was not able to solve this problem. Below is the list of my codes:

index.js


    import React, { useState, useEffect } from 'react';
    import Posts from './components/Pagination';
    import Pagination from './components/Post';
    import axios from 'axios';
    
    const Home = () => {
      const [posts, setPosts] = useState([]);
      const [loading, setLoading] = useState(false);
      const [currentPage, setCurrentPage] = useState(1);
      const [postsPerPage] = useState(10);
    
      useEffect(() => {
        const fetchPosts = async () => {
          setLoading(true);
          const res = await axios.get('https://jsonplaceholder.typicode.com/posts');
          setPosts(res.data);
          setLoading(false);
        };
    
        fetchPosts();
      }, []);
      // Get current posts
      const indexOfLastPost = currentPage * postsPerPage;
      const indexOfFirstPost = indexOfLastPost - postsPerPage;
      const currentPosts = posts.slice(indexOfFirstPost, indexOfLastPost);
    
      // Change page
      const paginate = pageNumber => setCurrentPage(pageNumber);
      return (
        <div
          style={{
            display: 'flex',
            height: '90vh'
          }}
        >
          <img src={require('../images/top_img.jpg')} alt='logo' height='500px' width='100%'/>
          <div className='container mt-5'>
          <h1 className='text-primary mb-3'>LATEST NEWS</h1>
          <Posts posts={currentPosts} loading={loading} />
          <Pagination
            postsPerPage={postsPerPage}
            totalPosts={posts.length}
            paginate={paginate}
          />
        </div>
        </div>
    
      );
    };
    
    export default Home;

Post.js


    import React from 'react';
    
    const Posts = ({ posts, loading }) => {
      if (loading) {
        return <h2>Loading...</h2>;
      }
    
      return (
        <ul className='list-group mb-4'>
          {posts.map(post => (
            <li key={post.id} className='list-group-item'>
              {post.title}
            </li>
          ))}
        </ul>
      );
    };
    
    export default Posts;

Pagination.js


    import React from 'react';
    
    const Posts = ({ posts, loading }) => {
      if (loading) {
        return <h2>Loading...</h2>;
      }
    
      return (
        <ul className='list-group mb-4'>
          {posts.map(post => (
            <li key={post.id} className='list-group-item'>
              {post.title}
            </li>
          ))}
        </ul>
      );
    };
    
    export default Posts;

Below is the structure of my code:

The structure of my code

1
  • you swapped your component names in your imports Commented Mar 17, 2021 at 2:19

3 Answers 3

2

I believe your object names are swapped.

Sign up to request clarification or add additional context in comments.

Comments

0

Isn't there an error of relative path?

    import Posts from '../components/Pagination';
    import Pagination from './components/Post';

both imports are in the same file and both files are in the same folder, but one has ../ and other ./

2 Comments

I tried both import Posts from ` '../components/Pagination';, import Pagination from '../components/Post'; ` and import Posts from './components/Pagination'; import Pagination from './components/Post';. Both did not work .
@AbubakarOluyinka you pasted the same piece of code in Post.js and Pagination.js can you edit and put the correct code in Pagination.js maybe the problem is there! Another thing, you swapped the objects imported too. Change to this import Pagination from '../components/Pagination'; import Post from './components/Post';
0

First, you I would make the named imports match the file structure:

import Post from "./components/Post"
import Pagination from "./components/Pagination"

Also, your default exports are messing things up as well. You will want to export Pagination and then in ./Post you want to export Post.

Lastly, your paths were messed up. The "components" folder is on the same level as index.js. Thus, you can access it through "./components"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.