Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2022
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. POSTS.JS
  2. import React from 'react'
  3. import { Grid, Typography } from '@mui/material';
  4. import Project from './Project'; // not important
  5.  
  6. const Posts = ({posts}) => {
  7.  
  8. return (
  9. <div>
  10.         <Typography variant="h2">Blog posts</Typography>
  11.  
  12.         <Grid container spacing={5} sx={{
  13.         marginTop:2,
  14.         }}>
  15.  
  16.         {
  17.             posts.map(post =>{
  18.                 console.log("log: ", post.id); // first case scenario (pasted correctly in stackoverflow post)
  19.                 <Grid>Hello</Grid> // second case scenario (pasted correctly in stackoverflow post)
  20.             })
  21.         }
  22.  
  23.         </Grid>
  24.     </div>
  25.   )
  26. }
  27.  
  28. export default Posts
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. APP.JS
  39. import { Button, Paper, Container, Grid, Typography, Box } from '@mui/material';
  40. import Posts from "./comp/Posts";
  41. import React, { useEffect, useState } from 'react'
  42. const POSTS_API_URL = 'http://localhost:8000/api/test/';
  43.  
  44. function App() {
  45.   const [posts, setPosts] = useState([]);
  46.  
  47.       const getPosts = async () =>{
  48.         const response = await fetch(`${POSTS_API_URL}`);
  49.         const data = await response.json();
  50.         setPosts(data);
  51.     }
  52.  
  53.     useEffect(()=>{
  54.         getPosts();
  55.     }, []);
  56.  
  57.  
  58.   return (
  59.     <div className="App">
  60.         <Container maxWidth="lg">
  61.         <Paper elevation="2" square sx={{
  62.           display: "flex",
  63.           justifyContent: "space-between",
  64.           padding: 1,
  65.           marginTop: 2,
  66.           marginBottom: 2,
  67.         }}>
  68.         <Box>
  69.           <Button variant="primary" color="primary">Home</Button>
  70.           <Button variant="primary" color="primary">About Me</Button>
  71.         </Box>
  72.         <Box>
  73.           <Button variant="contained" color="primary">Contact</Button>
  74.         </Box>
  75.         </Paper>
  76.  
  77.  
  78. {/* Posts here */}
  79.         <Posts posts={posts}/>
  80. {/* Posts here */}
  81.        
  82.         </Container>
  83.     </div>
  84.   );
  85. }
  86.  
  87. export default App;
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement