Text will not populate in React Grid - reactjs

I am trying to create 4 react panels using Grid to showcase my skills for my Portfolio application. My text was working fine but, once I tried to implement a custom Paper Grid from material UI the text is being overridden somehow and I'm not sure why.
Here is my code where I declare the custom Grid:
import React from "react";
import { makeStyles} from "#material-ui/core/styles";
import Grid from "#material-ui/core/Grid";
import Paper from "#material-ui/core/Paper";
const useStyles = makeStyles((theme) => ({
root: {
flexGrow: 1,
color:'green',
},
paper: {
height: 150,
width: 100
},
}));
const SpacingGrid = () => {
const [spacing] = React.useState(2);
const classes = useStyles();
return (
<theme>
<Grid container className={classes.root} spacing={2}>
<Grid item xs={12}>
<Grid container justify="center" spacing={spacing}>
<Paper className={classes.paper} />
</Grid>
</Grid>
</Grid>
<Grid item xs={12}>
<Paper className={classes.control}>
<Grid container>
<Grid item></Grid>
</Grid>
</Paper>
</Grid>
</theme>
);
};
export default SpacingGrid;
Here is my code where I call the Grid and use it to implement my skills:
import React from 'react'
import Panel from '../accessories/Panels'
import SpacingGrid from '../accessories/Grid'
const frontEnd =['Django','Reactjs','Javascript','HTML','CSS']
const backEnd =['Python','Typescript','Nodejs','Express']
const databases =['MongoDB','MySQL']
const tools = ['AWS','AWS-CDK','Docker','AWS-Lambda','Figma','Microservices','Kubernetes']
//I should probably make my own styled components for this but NEED to learn how
const Skills = () =>{
return(
<div>
<h1 color="whitePrimary" align="center">
Skills
</h1>
<SpacingGrid>
<Panel flexWidth="25" padding="1" rounded>
<h1 color="whitePrimary" align="center">
Front-End
</h1>
<p color="whitePrimary" align="center">
</p>
{frontEnd.map((item) =>(
<p color="textPrimary" align="center" key={item}>
{item}
</p>
))}
</Panel>
<Panel flexWidth="31" padding="1" rounded>
<h1 color="whitePrimary" align="center">
Back-End
</h1>
<p color="whitePrimary" align="center">
</p>
{backEnd.map((item) => (
<p color="textPrimary" align="center" key={item}>
{item}
</p>
))}
</Panel>
<Panel flexWidth="31" padding="1" rounded>
<h1 color="whitePrimary" align="center">
Databases:
</h1>
<p color="whitePrimary" align="center">
</p>
{databases.map(((item) =>(
<p color="textPrimary" align="center" key={item}>
{item}
</p>
)))}
</Panel>
<Panel flexWidth="31" padding="1" rounded>
<h1 color="whitePrimary" align="center">
General Tooling:
</h1>
<p color="whitePrimary" align="center">
</p>
{tools.map((item => (
<p color="textPrimary" align="center" key={item}>
{item}
</p>
)))}
</Panel>
</SpacingGrid>
</div>
)
}
export default Skills;
Here is my App.js my file were all the components sit:
import {React, Suspense} from 'react'
import SocialFooter from '../components/SocialMediaFooter'
import Header from '../components/Header'
import Skills from '../components/SkillsPanels'
const Main = () =>{
return(
<div>
<Suspense fallback={<div>Loading...</div>}>
<Header />
</Suspense>
<Suspense fallback={<div>Loading...</div>}>
<Skills />
</Suspense>
<Suspense fallback={<div>Loading... </div>}>
<SocialFooter />
</Suspense>
</div>
)}
//add all react components here and export to app.js
export default Main;
My issue is that I believe the error to be in the Grid.js file. Now the code works but, it does not add the text to the app as I have it in SkillsPanels.js it just gives me blank Grids which leaves me stumped. Thank you all!

I passed props in the SpacingGrid function and then called {props.children} next to

Related

TypeError: Cannot read properties of undefined (reading 'name') in React

i am getting the below error
TypeError: Cannot read properties of undefined (reading 'name') in react
data is not coming from the Products.
i am learning the react now and this is my first project so could you please help to sort out this issue.
index.js
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById("root")
);
App.js
import React from "react";
import Products from "./Components/Products/Products";
const App = () => {
return (
<div>
<Products />
</div>
);
};
export default App;
Products.jsx
import React from "react";
import { Grid } from "#material-ui/core";
import Product from "./Product/Product";
const products = [
{
id: 1,
name: "Macbook Air",
description: "Apple Macbook Air",
price: "$999",
},
{
id: 2,
name: "Macbook Pro",
description: "Apple Macbook Pro",
price: "$1199",
},
];
const Products = () => {
return (
<main>
<Grid container justify="center" spacing={4}>
{products.map((product) => (
<Grid item key={product.id} xs={12} sm={6} md={4} lg={3}>
<Product />
</Grid>
))}
</Grid>
</main>
);
};
export default Products;
Product.jsx
import React from "react";
import {
Card,
CardMedia,
CardContent,
CardActions,
Typography,
IconButton,
} from "#material-ui/core";
import { AddShoppingCart } from "#material-ui/icons";
import useStyles from "./styles";
const Product = ({ product }) => {
const classes = useStyles();
return (
<Card className={classes.root}>
<CardMedia className={classes.media} image="" title={product.name} />
<CardContent>
<div className={classes.cardContent}>
<Typography variant="h5" gutterBottom>
{product.name}
</Typography>
<Typography variant="h5">{product.price}</Typography>
</div>
<Typography variant="h2" color="textSecondary">
{product.description}
</Typography>
</CardContent>
<CardActions disableSpacing className={classes.cardActions}>
<IconButton aria-label="Add to cart">
<AddShoppingCart />
</IconButton>
</CardActions>
</Card>
);
};
export default Product;
How can i solve the above problem
For reference please find the attached image
You didn't pass the product prop to your <Product/> component, so it's undefined by default, and then you're referencing the property name in the Product component of an undefined prop, therefore you get the error.
Make the following change
const Products = () => {
return (
<main>
<Grid container justify="center" spacing={4}>
{products.map((product) => (
<Grid item key={product.id} xs={12} sm={6} md={4} lg={3}>
<Product product={product}/>
</Grid>
))}
</Grid>
</main>
);
};
It appears you are not passing the product as a prop to you <Product /> component.
Add this to your map:
{products.map((product) => (
<Grid item key={product.id} xs={12} sm={6} md={4} lg={3}>
<Product product={product}/>
</Grid>
))}
need to pass prop to your component
{products.map((product) => (
<Grid item key={product.id} xs={12} sm={6} md={4} lg={3}>
<Product product={product}/>
</Grid>
))}

Retrieve image url from database and display in React using Material-UI

I have a list of movies and I want to display the name of the movie and the image attached to that movie. I got image URLs from the web and put them inside an MySQL database(varchar300).
const MovieView = (props: { movie: Movie; }) => {
const {movie} = props;
return (
<Grid item xs={12} sm={6} md={3}
className="movie">
<Link to={`movies/${movie.id}`}>
<Paper elevation={3} className="movie-paper">
<div>
<h2>{movie.title}</h2>
<img src="movie.image" alt=""/>
</div>
</Paper>
</Link>
</Grid>
);
In order to do this, you need the src to be in template form (using squiggly brackets {}) instead of quotes. Try this instead
const MovieView = (props: { movie: Movie; }) => {
const {movie} = props;
return (
<Grid item xs={12} sm={6} md={3}
className="movie">
<Link to={`movies/${movie.id}`}>
<Paper elevation={3} className="movie-paper">
<div>
<h2>{movie.title}</h2>
<img src={movie.image} alt=""/>
</div>
</Paper>
</Link>
</Grid>
);
}

React, Link to component wont load until page is refreshed

React, Link to component wont load until page is refreshed
My Link for terms and conditions won't load until the page is refreshed view the video below here.
https://youtu.be/2QumWBRXiuM
Here is the Footer.js code
The link to the page should be on line 44 I have it in a link tag than a list (li) tag.
import React from "react";
// import { Button } from "./Button";
import "./Footer.css";
import { Text, Div, Button, Row, Col, Container, Image } from "atomize";
import logo from "../logo/tryb_logo_medium.png";
import { Link } from "react-router-dom";
import TwitterIcon from '#material-ui/icons/Twitter';
import FacebookIcon from '#material-ui/icons/Facebook';
import InstagramIcon from '#material-ui/icons/Instagram';
import { Icon } from "#material-ui/core";
function Footer() {
return (
<div className="main-footer" id="onTop">
<Container>
<Row d="flex">
{/* Col 1
<div className="col-sm">
<section className="footer-subscription">
<p className="footer-subscription-heading">Join the Tryb</p>
<div className="input-areas">
<form>
<input
type="email"
name="email"
placeholder="Your email"
className="footer-input"
/>
</form>
<p className="footer-subscription-text">
Unsubscribe at any time
</p>
<Button>Subscribe</Button>
</div>
</section>
</div> *}
{/* Col 2 */}
<Col textColor="white" size={{ xs: '12', md: '4' }}>
<div text-align="center" p="1rem">
<h4>Need Help?</h4>
<ul className="list-unstyled">
<li>Contact Us</li>
<Link to="/termsandconditions">
<li>Terms & Conditions</li>
</Link>
</ul>
</div>
</Col>
{/* Col 4 */}
<Col size={{ xs: '12', md: '4' }} align="center">
<div p="1rem">
<h4>Social</h4>
<ul className="list-unstyled">
<a href="https://www.facebook.com/trybprints" target="_blank" ><FacebookIcon/></a>
<a href="https://www.instagram.com/tryb_prints/" target="_blank" ><InstagramIcon/></a>
<a href="https://twitter.com/PrintsTryb" target="_blank" ><TwitterIcon/></a>
</ul>
</div>
</Col>
{/* Col 3 */}
<Col size={{ xs: '12', md: '4' }}>
<div p="1rem">
<ul className="list-unstyled">
<li> <img
src={logo}
alt="tryb logo"
width="300px"
className="d-inline-block align-top"
/></li>
<li></li>
</ul>
</div>
</Col>
</Row>
</Container>
<hr />
<div className="copyright">
Copyright © Tryb Prints {new Date().getFullYear()}
</div>
</div>
);
};
export default Footer;
any help at all is greatly appreciated thanks
I can't recreate the problem, and would need to see more code to be more help (would like to see inside the <Switch>...</Switch> and inside the "TermsAndConditions" component).
You can maybe try to use useHistory? Something like this:
import { Link, useHistory } from "react-router-dom";
// ...
function Footer() {
const history = useHistory();
const handleClick = () => {
history.push("/termsandconditions");
}
return (
//...
// Remove the <Link> here
<a onClick={handleClick}>
<li>Terms & Conditions</li>
</a>
//...

Display my cards in 3 columns using reactjs Material ui

I'm trying to retrieve images from an API and display them in a three column grid system. When I retrieved images, they are displayed one beneath each other.
Kindly advised best way to achieve this
import React from "react";
import Display from "./Display";
class App extends React.Component {
constructor() {
super();
this.state = {
loading: true,
image: [],
};
}
async componentDidMount() {
const url = "http://jsonplaceholder.typicode.com/photos?_start=0&_limit=10";
const response = await fetch(url);
const data = await response.json();
this.setState({ image: data, loading: false });
console.log(data);
}
render() {
if (this.state.loading) {
return <div> loading ... </div>;
}
if (!this.state.image.length) {
return <div> didnt get an image</div>;
}
return (
<div>
{this.state.image.map((img) => (
<div>
<div>
{" "}
<Display
showImage={img.url}
showTitle={img.title}
showAlbum={img.albumId}
/>{" "}
</div>
<div key={img.id}>
<ul></ul>
</div>
</div>
))}
</div>
);
}
}
export default App;
function Display(props) {
const classes = useStyles();
return (
<div className={classes.root}>
<Grid container spacing={4}>
<Grid item xs={12} sm={6} md={4}>
<CardActionArea>
<CardMedia
component="img"
alt="Contemplative Reptile"
height="200"
width="200"
img
src={props.showImage}
/>
<CardContent>
<Typography gutterBottom variant="h5" component="h2">
{props.showTitle}
</Typography>
<Typography variant="body2" color="textSecondary" component="p">
Album ID: {props.showAlbum}
</Typography>
</CardContent>
</CardActionArea>
<CardActions>
<Button size="small" color="primary">
Share
</Button>
<Button size="small" color="primary">
Learn More
</Button>
</CardActions>
</Grid>
</Grid>
</div>
);
}
export default Display;
In my App.js I'm able to using the map feature and get all the items from the array, I'm thinking I should modify my App.jS file to print out the results in the three columns but I'm not sure. Can I modify the Display file so that each card goes right beside each other? Do I need to use another array for the cards?
You can add display flex to the parent div in your render method as default flexDirection = 'row'
render() {
if (this.state.loading) {
return <div> loading ... </div>;
}
if (!this.state.image.length) {
return <div> didnt get an image</div>;
}
return (
<div style={{display:"flex"}}> //added display flex
{this.state.image.map((img) => (
<div>
<div>
{" "}
<Display
showImage={img.url}
showTitle={img.title}
showAlbum={img.albumId}
/>{" "}
</div>
<div key={img.id}>
<ul></ul>
</div>
</div>
))}
</div>
);
}

react slick slider convert class component to functional component

i am using react slick...and using class component, now i just want to convert class component to functional component....because in these component i need to use another function component...it getting some problem....could any one help on this thing...below the class i have used (renderArrows =) this i thing is getting problem...
this is the link of code sand box : https://codesandbox.io/s/jovial-smoke-ndzhj
import React, {Component } from 'react';
import { Col, Row } from 'react-grid-system';
import { ButtonBase } from '#material-ui/core';
import ArrowBackIosIcon from '#material-ui/icons/ArrowBackIos';
import ArrowForwardIosIcon from '#material-ui/icons/ArrowForwardIos';
import Slider from "react-slick";
class Example extends Component {
renderArrows = () => {
return (
<div className="slider-arrow">
<ButtonBase
className="arrow-btn prev"
onClick={() => this.slider.slickPrev()}
>
<ArrowBackIosIcon />
</ButtonBase>
<ButtonBase
className="arrow-btn next"
onClick={() => this.slider.slickNext()}
>
<ArrowForwardIosIcon />
</ButtonBase>
</div>
);
};
render() {
return (
<div>
<Col xl={12} lg={12} md={12} sm={12} xs={12} className="desktop-slider">
<div className="education-level main-boxes boxex-bottom">
<Row className="row">
<Col xl={4} lg={4} md={4} sm={12} xs={12}>
<div className="index-box-head horizontal-box-head">
1. Education Level
</div>
</Col>
<Col xl={8} lg={8} md={8} sm={12} xs={12}>
<div style={{ position: "relative" }}>
{this.renderArrows()}
<Slider
ref={c => (this.slider = c)}
dots={false}
arrows={false}
centerMode={true}
slidesToShow={1}
infinite={false}>
<li>Home
</li>
<li>About Us
</li>
<li>Gallery
</li>
</Slider>
</div>
</Col>
</Row>
</div>
</Col>
</div>
);
}
}
export default Example;
This can be the best answer
I tried and made this work with you sample
Check please
https://codesandbox.io/s/infallible-turing-wgvrb?file=/src/App.js
import React, { useRef } from "react";
import { Col, Row } from "react-grid-system";
import { ButtonBase } from "#material-ui/core";
import ArrowBackIosIcon from "#material-ui/icons/ArrowBackIos";
import ArrowForwardIosIcon from "#material-ui/icons/ArrowForwardIos";
import Slider from "react-slick";
const Example = () => {
const customSlider = useRef();
const renderArrows = () => {
return (
<div className="slider-arrow">
<ButtonBase
className="arrow-btn prev"
onClick={() => customSlider.current.slickPrev()}
>
<ArrowBackIosIcon />
</ButtonBase>
<ButtonBase
className="arrow-btn next"
onClick={() => customSlider.current.slickNext()}
>
<ArrowForwardIosIcon />
</ButtonBase>
</div>
);
};
return (
<div>
<Col xl={12} lg={12} md={12} sm={12} xs={12} className="desktop-slider">
<div className="education-level main-boxes boxex-bottom">
<Row className="row">
<Col xl={4} lg={4} md={4} sm={12} xs={12}>
<div className="index-box-head horizontal-box-head">
1. Education Level
</div>
</Col>
<Col xl={8} lg={8} md={8} sm={12} xs={12}>
<div style={{ position: "relative" }}>
{renderArrows()}
<Slider
ref={slider => (customSlider.current = slider)}
dots={false}
arrows={false}
centerMode={true}
slidesToShow={1}
infinite={false}
>
<li>Home</li>
<li>About Us</li>
<li>Gallery</li>
</Slider>
</div>
</Col>
</Row>
</div>
</Col>
</div>
);
};
export default Example;
class component to functional component:
import React, { Component } from 'react'
import { Col, Row } from 'react-grid-system'
import { ButtonBase } from '#material-ui/core'
import ArrowBackIosIcon from '#material-ui/icons/ArrowBackIos'
import ArrowForwardIosIcon from '#material-ui/icons/ArrowForwardIos'
import Slider from 'react-slick'
const renderArrows = () => {
return (
<div className="slider-arrow">
<ButtonBase
className="arrow-btn prev"
onClick={() => this.slider.slickPrev()}
>
<ArrowBackIosIcon />
</ButtonBase>
<ButtonBase
className="arrow-btn next"
onClick={() => this.slider.slickNext()}
>
<ArrowForwardIosIcon />
</ButtonBase>
</div>
)
}
const Example = () => {
return (
<div>
<Col xl={12} lg={12} md={12} sm={12} xs={12} className="desktop-slider">
<div className="education-level main-boxes boxex-bottom">
<Row className="row">
<Col xl={4} lg={4} md={4} sm={12} xs={12}>
<div className="index-box-head horizontal-box-head">
1. Education Level
</div>
</Col>
<Col xl={8} lg={8} md={8} sm={12} xs={12}>
<div style={{ position: 'relative' }}>
{renderArrows()}
<Slider
ref={c => (this.slider = c)}
dots={false}
arrows={false}
centerMode={true}
slidesToShow={1}
infinite={false}
>
<li>Home</li>
<li>About Us</li>
<li>Gallery</li>
</Slider>
</div>
</Col>
</Row>
</div>
</Col>
</div>
)
}
export default Example
One more thing, I don't seem to get from where this.slider.slickNext() came from. I think it would be this.Slider.slickPrev().
If I'm right, then for functional component, change it to Slider.slickPrev()

Resources