0

I set up a logic to create pagination in react using data that I fetched from an endpoint API, which contains 5 objects that I displayed in this UI, Then I created a function pageCount that I implemented into my react paginate component, when I click to move to the second page it moved to the last page, next, and previously they didn't work either.

I research a lot to fix the problem but I didn't find any solutions?

Where this problem comes from?

Thank you

function App() {
  
  //states
  const [yogaCourses, setYogaCourses] = useState([]);
  const [isLoading, setIsLoading] = useState(true);
  const [levels, setLevels] = useState([]);
  const [level, setLevel] = useState('');
  const [pageNumber, setPageNumber] = useState(0);

  //Paginate react 
  const coursePerPage = 2;

  // const PageVisited = numberPage * coursePerPage;
  const pageCount = Math.ceil(yogaCourses.length / coursePerPage);
  console.log(pageCount);

  //Track changes on each numberPage and display the data 
  useEffect(()=> {
    setYogaCourses(yogaCourses.slice(pageNumber * 2 , (pageNumber + 1) * 2))
  },[pageNumber]);

  //To the next Page
  const pageChange = ({selected}) => {
    setPageNumber(selected);
  }

  //levelChangeHandler 
  const levelChangeHandler = ({value}) => {
      setLevel(value);
  }

  //Filter by Levels // stateless
  const filterLevels = (level) => {
    return yogaCourses.filter((singleLevel)=> level ? singleLevel.level === level : true);
  }

  //Function to fetch the data from the API
  const GetCourses = async () => {
    const response = await axios.get(url)
    const {data} = response;
    return data;
  }

//UseEffect to run the function on every render
  useEffect(()=> {
    const GetCoursesYoga = async () => {
      const result = await GetCourses();
      setYogaCourses(result);
      setLevels(Array.from(new Set(result.map((result)=> result.level))));
    } 
    GetCoursesYoga();
  }, []);


  //check if the we got response
  useEffect(()=> {
    if(yogaCourses.length > 0) {
      setIsLoading(false);
    }
  }, [yogaCourses])
  

  if(isLoading) {
    return (
      <Loading/>
    )
  } 
  else {
    return (
      <main>
        <div className="title">
                <h2>YOUR PRACTICE REIMAGINED</h2>
            </div>
        <LevelsFilter levels={levels} onChange={levelChangeHandler}/>
        <YogaCourses yogaCourses= {(filterLevels(level)).slice(0, 2)}/>
        <ReactPaginate
        previousLabel = {"Previous"}
        nextLabel = {"Next"}
        pageCount = {pageCount}
        onPageChange= {pageChange}
        />
      </main>
      );
  }

}

export default App;
Janez Kuhar
  • 3,705
  • 4
  • 22
  • 45
David Jay
  • 345
  • 1
  • 2
  • 15
  • 1
    why make pagination on multiple pages? just use the same page, and just update the content. – Ion Utale May 17 '21 at 12:13
  • what do you mean? I have 5 items in total and I want to display just 2 per page and move from the next 2 then the last one! – David Jay May 17 '21 at 12:27
  • 1
    I guess when you change to next page you are also changing `yogaCourses` array so it will rerender and then your pageCount will change to `1` so once your pageCount becomes `1` it just shows 1 page only. – Priyank Kachhela May 17 '21 at 12:33
  • Thank you so much for your explanation, can you please demonstrate this with code !! – David Jay May 17 '21 at 12:41

0 Answers0