How to fix react paginate problem by moving from page to another

2k Views Asked by At

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;
0

There are 0 best solutions below