How to save values from api as a number REACT js - reactjs

This is the api I am using https://covid19.mathdro.id/api
I need to take the {recovered.value} and divide it by {confirmed.value} and then multiply the result by 100 to get the recovery percentage.
But these values are coming as string and not number//
this is the file
import React from "react";
import { Card, CardContent, Typography, Grid } from "#material-ui/core";
import CountUp from "react-countup";
import cx from "classnames";
import styles from "./Cards.module.css";
const Info = ({ data: { confirmed, recovered, deaths, lastUpdate } }) => {
if (!confirmed) {
return "Loading...";
}
return (
<div className={styles.container}>
<Grid container spacing={3} justify="center">
<Grid
item
xs={12}
md={3}
component={Card}
className={cx(styles.card, styles.infected)}
>
<CardContent>
<Typography color="textSecondary" gutterBottom>
Infected
</Typography>
<Typography variant="h5" component="h2">
<CountUp
start={0}
end={confirmed.value}
duration={2.75}
separator=","
/>
<div>
{recovered.value}/{confirmed.value}
</div>
</Typography>
<Typography color="textSecondary">
{new Date(lastUpdate).toDateString()}
</Typography>
<Typography variant="body2" component="p">
Number of active cases of COVID-19.
</Typography>
</CardContent>
</Grid>
<Grid
item
xs={12}
md={3}
component={Card}
className={cx(styles.card, styles.recovered)}
>
<CardContent>
<Typography color="textSecondary" gutterBottom>
Recovered
</Typography>
<Typography variant="h5" component="h2">
<CountUp
start={0}
end={recovered.value}
duration={2.75}
separator=","
/>
</Typography>
<Typography color="textSecondary">
{new Date(lastUpdate).toDateString()}
</Typography>
<Typography variant="body2" component="p">
Number of recoveries from COVID-19.
</Typography>
</CardContent>
</Grid>
<Grid
item
xs={12}
md={3}
component={Card}
className={cx(styles.card, styles.deaths)}
>
<CardContent>
<Typography color="textSecondary" gutterBottom>
Deaths
</Typography>
<Typography variant="h5" component="h2">
<CountUp
start={0}
end={deaths.value}
duration={2.75}
separator=","
/>
</Typography>
<Typography color="textSecondary">
{new Date(lastUpdate).toDateString()}
</Typography>
<Typography variant="body2" component="p">
Number of deaths caused by COVID-19.
</Typography>
</CardContent>
</Grid>
</Grid>
</div>
);
};
export default Info;
and I have tried parseInt, in this part of code,for confirmed, but it does not woke
const Info = ({ data: { parseInt(confirmed), recovered, deaths,
lastUpdate } }) => {
if (!confirmed) {
return "Loading...";
}
....
this is my app.js
import React from "react";
import { Cards, CountryPicker, Chart } from "./components";
import { fetchData } from "./api/";
import styles from "./App.module.css";
class App extends React.Component {
state = {
data: {},
country: "",
};
componentWillUpdate() {
console.log("hello");
}
async componentDidMount() {
const data = await fetchData();
this.setState({ data });
console.log("data");
}
handleCountryChange = async (country) => {
const data = await fetchData(country);
this.setState({ data, country: country });
console.log("data");
};
render() {
const { data, country } = this.state;
return (
<div className={styles.container}>
<Cards data={data} />
<CountryPicker handleCountryChange={this.handleCountryChange} />
<Chart data={data} country={country} />
</div>
);
}
}
export default App;

You have bug on your code. I tried to fixed it and able to run it. Please check the update code here:
import React from "react";
import {Card, CardContent, Typography, Grid} from "#material-ui/core";
import CountUp from "react-countup";
import cx from "classnames";
import {makeStyles, useTheme} from '#material-ui/core/styles';
const useStyles = makeStyles(theme => ({
container: {},
infected: {},
recovered: {},
deaths: {}
}));
const Info = ({data: {confirmed, recovered, deaths, lastUpdate}}) => {
const styles = useStyles();
return (
<div className={styles.container}>
<Grid container spacing={3} justify="center">
<Grid
item
xs={12}
md={3}
component={Card}
className={cx(styles.card, styles.infected)}
>
<CardContent>
<Typography color="textSecondary" gutterBottom>
Infected
</Typography>
<Typography variant="h5" component="h2">
<CountUp
start={0}
end={confirmed.value}
duration={2.75}
separator=","
/>
<div>
{recovered.value}/{confirmed.value}
</div>
</Typography>
<Typography color="textSecondary">
{new Date(lastUpdate).toDateString()}
</Typography>
<Typography variant="body2" component="p">
Number of active cases of COVID-19.
</Typography>
</CardContent>
</Grid>
<Grid
item
xs={12}
md={3}
component={Card}
className={cx(styles.card, styles.recovered)}
>
<CardContent>
<Typography color="textSecondary" gutterBottom>
Recovered
</Typography>
<Typography variant="h5" component="h2">
<CountUp
start={0}
end={recovered.value}
duration={2.75}
separator=","
/>
</Typography>
<Typography color="textSecondary">
{new Date(lastUpdate).toDateString()}
</Typography>
<Typography variant="body2" component="p">
Number of recoveries from COVID-19.
</Typography>
</CardContent>
</Grid>
<Grid
item
xs={12}
md={3}
component={Card}
className={cx(styles.card, styles.deaths)}
>
<CardContent>
<Typography color="textSecondary" gutterBottom>
Deaths
</Typography>
<Typography variant="h5" component="h2">
<CountUp
start={0}
end={deaths.value}
duration={2.75}
separator=","
/>
</Typography>
<Typography color="textSecondary">
{new Date(lastUpdate).toDateString()}
</Typography>
<Typography variant="body2" component="p">
Number of deaths caused by COVID-19.
</Typography>
</CardContent>
</Grid>
</Grid>
</div>
);
};
export default Info;
You should should use Info Component in your app component like below:
import React from 'react';
import Info from "./Info";
class App extends React.Component {
render() {
let data = {
confirmed: {value: 245550},
recovered: {value: 4555},
deaths: {value: 4534},
lastUpdate: Date.now()
};
return (
<Info data={data}/>
)
}
}
export default App;

Related

Uncaught TypeError: Cannot read properties of undefined (reading 'source') react v16

I'm trying to access data from commerceJS API fetched on a different module and passed down as a prop, the code only breaks when I call a nested product object with this error Uncaught TypeError: Cannot read properties of undefined (reading 'source').
Here is my code
//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, onAddToCart }) => {
const classes = useStyles();
const handleAddToCart = () => onAddToCart(product.id, 1);
return (
<Card className={classes.root}>
<CardMedia
className={classes.media}
image={product.media.source}
component="img"
title={product.name}
/>
<CardContent>
<div className={classes.cardContent}>
<Typography gutterBottom variant="h5" component="h2">
{product.name}
</Typography>
<Typography gutterBottom variant="h5" component="h2">
${product.price.formatted}
</Typography>
</div>
<Typography
dangerouslySetInnerHTML={{ __html: product.description }}
variant="body2"
color="textSecondary"
component="p"
/>
</CardContent>
<CardActions disableSpacing className={classes.cardActions}>
<IconButton aria-label="Add to Cart" onClick={handleAddToCart}>
<AddShoppingCart />
</IconButton>
</CardActions>
</Card>
);
};
export default Product;
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, onAddToCart }) => {
const classes = useStyles();
const handleAddToCart = () => onAddToCart(product.id, 1);
return (
<Card className={classes.root}>
<CardMedia
className={classes.media}
image={product.media.source}
component="img"
title={product.name}
/>
<CardContent>
<div className={classes.cardContent}>
<Typography gutterBottom variant="h5" component="h2">
{product?.name}
</Typography>
<Typography gutterBottom variant="h5" component="h2">
${product?.price?.formatted}
</Typography>
</div>
<Typography
dangerouslySetInnerHTML={{ __html: product.description }}
variant="body2"
color="textSecondary"
component="p"
/>
</CardContent>
<CardActions disableSpacing className={classes.cardActions}>
<IconButton aria-label="Add to Cart" onClick={handleAddToCart}>
<AddShoppingCart />
</IconButton>
</CardActions>
</Card>
);
};
export default Product;
try this code:
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, onAddToCart }) => {
const classes = useStyles();
const handleAddToCart = () => onAddToCart(product.id, 1);
return (
<Card className={classes.root}>
<CardMedia
className={classes.media}
image={product?.media?.source}
component="img"
title={product?.name}
/>
<CardContent>
<div className={classes.cardContent}>
<Typography gutterBottom variant="h5" component="h2">
{product?.name}
</Typography>
<Typography gutterBottom variant="h5" component="h2">
${product?.price?.formatted}
</Typography>
</div>
<Typography
dangerouslySetInnerHTML={{ __html: product?.description }}
variant="body2"
color="textSecondary"
component="p"
/>
</CardContent>
<CardActions disableSpacing className={classes.cardActions}>
<IconButton aria-label="Add to Cart" onClick={handleAddToCart}>
<AddShoppingCart />
</IconButton>
</CardActions>
</Card>
);
};
export default Product;
It'll display the image if I delete the className property and access to my object key
Code here:
<CardMedia
className={classes.media}
image={product.image.url}
component="img"
title={product?.name}
/>

material-ui horizontally align two cards below first card

How can I align Graph Two and Graph three horizontally below Graph One ?
In other words I want to leave Graph One card how it is but move Graph two so it is on the same level and Graph three and they are horizontally aligned with one another below Graph One
This is what I have so far
import React from "react";
import Grid from "#mui/material/Grid";
import Container from "#mui/material/Container";
import Box from "#mui/material/Box";
import Typography from "#mui/material/Typography";
import Card from "#mui/material/Card";
import CardActions from "#mui/material/CardActions";
import CardContent from "#mui/material/CardContent";
import Button from "#mui/material/Button";
import Paper from "#mui/material/Paper";
const styles = {
card: {
minWidth: 275,
display: "inline-block"
}
};
const YourCardOne = () => {
return (
<Card variant="outlined" style={{ height: "200%" }}>
<CardContent>
<Typography color="textSecondary" gutterBottom>
Graph One
</Typography>
<Typography variant="h5" component="h2">
Sarah Doria
</Typography>
<Typography color="textSecondary">Position</Typography>
<Typography variant="body2" component="p">
Company
<br />
{'"a benevolent smile"'}
</Typography>
</CardContent>
<CardActions></CardActions>
</Card>
);
};
const YourCardTwo = () => {
return (
<Card variant="outlined" style={{ height: "100%" }}>
<CardContent>
<Typography color="textSecondary" gutterBottom>
Graph Two
</Typography>
<Typography variant="h5" component="h2">
Sarah Doria
</Typography>
<Typography color="textSecondary">Position</Typography>
<Typography variant="body2" component="p">
Company
<br />
{'"a benevolent smile"'}
</Typography>
</CardContent>
<CardActions></CardActions>
</Card>
);
};
const YourCardThree = () => {
return (
<Card variant="outlined" style={{ height: "100%" }}>
<CardContent>
<Typography color="textSecondary" gutterBottom>
Graph Three
</Typography>
<Typography variant="h5" component="h2">
Sarah Doria
</Typography>
<Typography color="textSecondary">Position</Typography>
<Typography variant="body2" component="p">
Company
<br />
{'"a benevolent smile"'}
</Typography>
</CardContent>
<CardActions></CardActions>
</Card>
);
};
export default function GraphBackDrop() {
return (
<div>
<Container>
<Grid
container
spacing={3}
direction="row"
justify="center"
alignItems="stretch"
>
<Grid item xs={48}>
<Grid container spacing={25}>
<Grid item xs={12}>
<YourCardOne />
</Grid>
<Grid item xs={20} >
<YourCardTwo />
</Grid>
</Grid>
</Grid>
<Grid item xs={20}>
<YourCardThree />
</Grid>
</Grid>
</Container>
</div>
);
}
You can update your GraphBackDrop component to this:
export default function GraphBackDrop() {
return (
<Container>
<Grid
container
spacing={3}
justifyContent="center"
alignItems="stretch"
>
<Grid item xs={12}>
<YourCardOne />
</Grid>
<Grid item xs={12} sm={6}>
<YourCardTwo />
</Grid>
<Grid item xs={12} sm={6}>
<YourCardThree />
</Grid>
</Grid>
</Container>
);
}
Note: xs, sm, md, lg & xl are identified as breakpoints. It sets the number of columns the grid item uses. It can't be greater than the total number of columns of the container (12 by default).
If you want to learn more about MUI Grid component, refer to this official documentation.

How can I fix this problem while mapping in a card?

import * as React from 'react';
import Card from '#mui/material/Card';
import CardActions from '#mui/material/CardActions';
import CardContent from '#mui/material/CardContent';
import CardMedia from '#mui/material/CardMedia';
import Button from '#mui/material/Button';
import Typography from '#mui/material/Typography';
export default function MediaCard() {
return (
<Card sx={{ maxWidth: 345 }}>
{data.map((item) =>(
<CardMedia component="img" height="140" image={`${data.img}`} alt="home image looding......"/>
<CardContent>
<Typography gutterBottom variant="h5" component="div">{data.name}</Typography>
<Typography variant="body2" color="text.secondary">{data.description}</Typography>
</CardContent>
))}
<CardActions>
<Button size="small">Share</Button>
<Button size="small">Learn More</Button>
</CardActions>
</Card>
);
}
let data =[
{
name:'Property for sale',
img: '/home1.jpg',
description: 'This House is 5.5 cent with square feet area of 2000 and 3 km from palakkad city ;100 meters from main road.',
},
{
name:'Property for Rent',
img: '/home2.jpg',
title: 'Burger',
},
{
name:'Property for sale',
img: '/home3.jpg',
title: 'Camera',
},
{
name:'Property for Rent',
img: '/home4.jpg',
title: 'Coffee',
},
{
name:'Property for sale',
img: '/home5.jpg',
title: 'Hats',
},
{
name:'Property for Rent',
img: '/home7.jpg',
title: 'Honey',
},
];
I can't get the details of mapped items in the card. While doing this I had found an error
"ERROR in src\Components\Card\Card.js
Line 17:6: Parsing error: Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>? (17:6)."
I had used material UI and I don't understand whether the mistake had happened.
It should be wrapped in a parent element. e.g
export default function MediaCard() {
return (
<div>
<Card sx={{ maxWidth: 345 }}>
{data.map((item) =>(
<CardMedia component="img" height="140" image={`${data.img}`} alt="home image looding......"/>
<CardContent>
<Typography gutterBottom variant="h5" component="div">{data.name}</Typography>
<Typography variant="body2" color="text.secondary">{data.description}</Typography>
</CardContent>
))}
<CardActions>
<Button size="small">Share</Button>
<Button size="small">Learn More</Button>
</CardActions>
</Card>
</div>
);
}
Change this
{data.map((item) =>(
<CardMedia component="img" height="140" image={`${data.img}`} alt="home image looding......"/>
<CardContent>
<Typography gutterBottom variant="h5" component="div">{data.name}</Typography>
<Typography variant="body2" color="text.secondary">{data.description}</Typography>
</CardContent>
))}
to this
{data.map((item) =>(
<>
<CardMedia component="img" height="140" image={`${data.img}`} alt="home image looding......"/>
<CardContent>
<Typography gutterBottom variant="h5" component="div">{data.name}</Typography>
<Typography variant="body2" color="text.secondary">{data.description}</Typography>
</CardContent>
</>
))}
You should have wrapped the inner element of the map in an empty Tag and also wrap the whole component in an <> </> this empty tag

Getting “Cannot call a class as a function” in my React Project :throw new TypeError("Cannot call a class as a function");

I'm making COVID-19 tracker project and I got this error and I'm stuck, not sure where its coming from.
import { Card, CardContent, Typography, Grid } from '#material-ui/core';
import CountUp from 'react-countup';
import cx from 'react-countup';
import styles from './Cards.module.css';
const CardComponent = ({ className, cardTitle, value, lastUpdate, cardSubtitle }) => (
<Grid item xs={12} md={3} component={Card} className={cx(styles.card, className)}>
<CardContent>
<Typography color="textSecondary" gutterBottom>
{cardTitle}
</Typography>
<Typography variant="h5" component="h2">
<CountUp start={0} end={value} duration={2.75} separator="," />
</Typography>
<Typography color="textSecondary">
{new Date(lastUpdate).toDateString()}
</Typography>
<Typography variant="body2" component="p">
{cardSubtitle}
</Typography>
</CardContent>
</Grid>
);
const Info = ({ data: { confirmed, recovered, deaths, lastUpdate } }) => {
if (!confirmed) {
return 'Loading...';
}
return (
<div className={styles.container}>
<Typography gutterBottom variant="h4" component="h2">Global</Typography>
<Grid container spacing={3} justify="center">
<CardComponent
className={styles.infected}
cardTitle="Infected"
value={confirmed.value}
lastUpdate={lastUpdate}
cardSubtitle="Number of active cases from COVID-19."
/>
<CardComponent
className={styles.recovered}
cardTitle="Recovered"
value={recovered.value}
lastUpdate={lastUpdate}
cardSubtitle="Number of recoveries from COVID-19."
/>
<CardComponent
className={styles.deaths}
cardTitle="Deaths"
value={deaths.value}
lastUpdate={lastUpdate}
cardSubtitle="Number of deaths caused by COVID-19."
/>
</Grid>
</div>
);
};
export default Info;```
You are not returning a JSX element in your component but a simple string.
if (!confirmed) {
return 'Loading...';
}

How to map two different data values from api in react js

I am following https://github.com/adrianhajdin/project_corona_tracker
https://www.youtube.com/watch?v=khJlrj3Y6Ls
So in this project the following api is used-
https://covid19.mathdro.id/api/daily
here I am getting covid cases values for each day. I need to display the number of cases that increased from yesterday to today
so I am trying to get values of current date confirmed cases(total cases)- yesterday's date confirmed cases
this is my card.jsx file:
import React from "react";
import { Card, CardContent, Typography, Grid } from "#material-ui/core";
import CountUp from "react-countup";
import cx from "classnames";
import styles from "./Cards.module.css";
const Info = ({ data: { confirmed, recovered, deaths, lastUpdate } }) => {
if (!confirmed) {
return "Loading...";
}
return (
<div className={styles.container}>
<Grid container spacing={3} justify="center">
<Grid
item
xs={12}
md={3}
component={Card}
className={cx(styles.card, styles.infected)}
>
<CardContent>
<Typography color="textSecondary" gutterBottom>
Infected
</Typography>
<Typography variant="h5" component="h2">
<div>
---------------> total cases-yesterday's cases
</div>
<CountUp
start={0}
end={confirmed.value}
duration={2.75}
separator=","
/>
</Typography>
<Typography color="textSecondary">
{new Date(lastUpdate).toDateString()}
</Typography>
<Typography variant="body2" component="p">
Number of active cases of COVID-19.
</Typography>
</CardContent>
</Grid>
<Grid
item
xs={12}
md={3}
component={Card}
className={cx(styles.card, styles.recovered)}
>
<CardContent>
<Typography color="textSecondary" gutterBottom>
Recovered
</Typography>
<Typography variant="h5" component="h2">
<div>
{(
(Number(recovered.value) / Number(confirmed.value)) *
100
).toFixed(2)}{" "}
%
</div>
<CountUp
start={0}
end={recovered.value}
duration={2.75}
separator=","
/>
</Typography>
<Typography color="textSecondary">
{new Date(lastUpdate).toDateString()}
</Typography>
<Typography variant="body2" component="p">
Number of recoveries from COVID-19.
</Typography>
</CardContent>
</Grid>
<Grid
item
xs={12}
md={3}
component={Card}
className={cx(styles.card, styles.deaths)}
>
<CardContent>
<Typography color="textSecondary" gutterBottom>
Deaths
</Typography>
<Typography variant="h5" component="h2">
<div>
{(
(Number(deaths.value) / Number(confirmed.value)) *
100
).toFixed(2)}{" "}
%
</div>
<CountUp
start={0}
end={deaths.value}
duration={2.75}
separator=","
/>
</Typography>
<Typography color="textSecondary">
{new Date(lastUpdate).toDateString()}
</Typography>
<Typography variant="body2" component="p">
Number of deaths caused by COVID-19.
</Typography>
</CardContent>
</Grid>
</Grid>
</div>
);
};
export default Info;
Here's one way to extract that data:
fetch('https://covid19.mathdro.id/api/daily', {
method: 'GET'
}).then(res => res.json()).then(d => {
let todayData = d[d.length - 1];
let yesterdayData = d[d.length - 2];
console.log({
todayData
});
console.log({
yesterdayData
});
// minus total confirmed today - yesterday
const todayTotalDiff = todayData.totalConfirmed - yesterdayData.totalConfirmed;
console.log(`minus total confirmed today - yesterday = ${todayTotalDiff}`);
});

Resources