1

i am trying to figure out how i can use "const currentPosts". I am mapping trough all the fetched objects (pakketjes) and i am showing each of them. But i would like to only show the amount of objects that is stated in the const currentPosts. Here is the code

export default function HomePagina() {
  const paperStyle = { padding: '20px 20px', width: 600, margin: "20px auto", backgroundColor: "#F2D2BD", borderRadius: "35px" };
  const [pakketjes, setPakketjes] = React.useState([]);
  const [currentPage, setCurrentPage] = React.useState(2);
  const [postsPerPage, setpostsPerPage] = React.useState(2)
  const lastPostIndex = currentPage * postsPerPage;
  const firstPostIndex = lastPostIndex - postsPerPage;
  const currentPosts = pakketjes.slice(firstPostIndex, lastPostIndex)

  //useffect = voor bij pagina starten om info te loaden, in dit geval alle pakketjes tonen bij opstart
  React.useEffect(() => {
    fetch("http://localhost:8080/pakketje/getAll")
      .then(res => res.json())
      .then((result) => {
        setPakketjes(result);
      }
      )
  }, []);


 return (
    <>
      <Appbar />
      <br />
      <Container>
        <div>
          <img src={AllePakketjes} width="410" height="350" alt="" style={{ marginLeft: "365px" }} />
        </div>
        <h1 style={{ textAlign: "center", color: "white" }}>Alle pakketjes</h1>
        <Paper elevation={3} style={paperStyle}>
          {pakketjes.map(pakketje => (
            <Paper elevation={6} style={{ margin: "10px", padding: "15px", textAlign: "left", backgroundColor: "white", borderRadius: "25px" }} key={pakketje.id}>
              <b>Pakketje&emsp;&emsp;&emsp;&emsp;</b>
              <br /><br />
              <b>ID:</b>{pakketje.id}
              <br /><br />
              <b>Status: </b>{pakketje.status}
              <br />
              <b>Code:</b>{pakketje.code}
              <br /><br />
              <Button variant="contained" style={{ backgroundColor: "#AE0000" }} color="secondary" onClick={(e) => deletePakketje(pakketje.id, e)}>
                Delete <DeleteIcon fontSize="small" />
              </Button>&emsp;&emsp;
              <Link to={`/pakketjeUpdaten/${pakketje.id}`}>
                <Button variant="contained" style={{ backgroundColor: "Navy" }} color="secondary">
                  Update <ReplayIcon fontSize="small" />
                </Button>
              </Link>&emsp;&emsp;
              <Button variant="contained" style={{ backgroundColor: "black" }} color="secondary" onClick={(e) => statusOnderweg(pakketje.id, e)}>
                Verzenden&emsp;<LocalShippingIcon fontSize="small" />
              </Button>
              <br />
              <br />
            </Paper>
          ))
          }  
        </Paper>
      </Container>
    </>
  );
}
1
  • You never use currentPosts variable Commented Dec 16, 2022 at 18:55

1 Answer 1

1

It looks like you could just replace the pakketjes.map with currentPosts.map.

This will iterate through the subset of pakketjes specified by currentPosts rather than the entire array.

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

1 Comment

I have tried to do it like three times but i have placed the currentPosts in curly braces {}. It works perfectly fine without the curly braces, thank you sir!

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.