React Dynamic append using map - reactjs

I am new to react,
I am learning react, In this process I tried to create a for loop render using react. Were I am stuck in the following code
Below is my code
<Container>
<Row xs={3} md={4} lg={6}>
{[...Array(numberOfCards)].map((e, i) => {
return (
<Col className='mt-5' key={i+1}>
<Card >
{/* <Card.Img top width="100%" src="/images/companylogo.png" alt="Card image cap"/> */}
<Card.Body>
<Card.Title tag="h5">Card title</Card.Title>
<Card.Subtitle tag="h6" className="mb-2 text-muted">Card subtitle</Card.Subtitle>
<Card.Text>Some quick example text to build on the card title and make up the bulk of the
card's
content.</Card.Text>
<Button onClick={() =>addquantity({i+1},{email})}>Button</Button>
</Card.Body>
</Card>
</Col>
)
})}
</Row>
</Container>
here, In the Button Onclick event I am passing the "{i+1}"
Can you help me to understand what I am doing wrong here.
Thanks in advance

When you are defining the function in the onClick, the variables are in scope and shouldn't be inserted using {...} formats.
This should work (note that email should be defined somewhere):
<Container>
<Row xs={3} md={4} lg={6}>
{[...Array(numberOfCards)].map((e, i) => {
return (
<Col className='mt-5' key={i+1}>
<Card >
{/* <Card.Img top width="100%" src="/images/companylogo.png" alt="Card image cap"/> */}
<Card.Body>
<Card.Title tag="h5">Card title</Card.Title>
<Card.Subtitle tag="h6" className="mb-2 text-muted">Card subtitle</Card.Subtitle>
<Card.Text>Some quick example text to build on the card title and make up the bulk of the
card's
content.</Card.Text>
<Button onClick={() =>addquantity(i+1,email)}>Button</Button>
</Card.Body>
</Card>
</Col>
)
})}
</Row>
</Container>

Related

How to center content inside Row element

I am trying to center some cards inside a row element but I cant get it to work!
<Row className="d-flex justify-content-center">
{cardData.map((data) => {
return (
<Col>
<Card style={{ width: "18rem" }}>
<Card.Img variant="top" src={data.img} />
<Card.Body>
<Card.Title>{data.title}</Card.Title>
<Card.Text>{data.text}</Card.Text>
<Button variant="primary">{data.button}</Button>
</Card.Body>
</Card>
</Col>
);
})}
</Row>
this is how it looks:: (as you can see, there is un-event spacing on the left and right of the card
I tried to put the following classes (mx-auto,d-flex justify-content-center) on the parent row but I cant get it to work
any advice is greatly appreciated
use justify-content: space-between
<Row style={{display:'flex', justifyContent: 'space-between'}}>

How to display only to items in row?

I Don't know how to display only two elements in row and keep last Column empty, spend almost 9h to find resolve
Red zone has to be empty, here is react component of the card
return (
<Row gutter={[24,24]}>
{
name.map((news)=>(
<Col xs={24} sm={12} lg={8} key={news.id}>
{/*<div className={"card"}>*/}
<Card hoverable className={"news-card"} style={{height: "250px"}}>
<a href={news.url} target={"_blank"} rel={"noreferrer"}>
<div className="news-image-container">
<Title className={"news-title"} level={4}>{news.name}</Title>
<img style={{maxWidth: '200px', maxHeight: '100px'}} src={news.image} alt={"news"}/>
</div>
<p>
{
news.description.length > 100 ? `${news.description.substring(0, 100)}...`
:news.description
}
</p>
<div className="provider-container">
<Text>{moment(news.datePublished).startOf('ss').fromNow()}</Text>
</div>
</a>
{/*</div>*/}
</Card>
</Col>
))
}
</Row>
);

I want to run a if else condition inside the react map function. How to do this?

I want to use a condition inside the card title. How do I do it? I have tried this if statement but it does output the result. Everything else is fine.
this.state.projectList.map((project, index) =>(
<Card key={index} className='mb-3' >
<Card.Header>{project.name}{" "}
<Badge variant="primary">{project.tasks_count}</Badge>
</Card.Header>
<Card.Body>
<Card.Title>
if (project.status===0) {
<h2>Incomplete</h2>
}
</Card.Title>
<Card.Text>
{project.description}
</Card.Text>
<Button variant="primary">Go somewhere</Button>
</Card.Body>
</Card>
))
update your code like this
this.state.projectList.map((project, index) =>(
<Card key={index} className='mb-3' >
<Card.Header>{project.name}{" "}
<Badge variant="primary">{project.tasks_count}</Badge>
</Card.Header>
<Card.Body>
<Card.Title>
{project.status===0 ? <h2>Incomplete</h2> : null}
</Card.Title>
<Card.Text>
{project.description}
</Card.Text>
<Button variant="primary">Go somewhere</Button>
</Card.Body>
</Card>
))

I am Trying to center the card but its not working

I am building a react application in which I am trying to center the blocks,by using justify content center but its not working. I have used the justify-content-center, center -block ,but still its not working
Please Help me out
function Trackers(props) {
return (
<>
<h2 class="font-weight-bold text-center mt-4 mb-4" >COVID-19 TRACKER</h2>
<Container>
<Row >
<Col xs={12} sm={12} md={4} className="center-block">
<Card style={{ width: '15rem' }} className="justify-content-center text-center ml-1 mr-1 mt-1 mb-1" >
<Card.Body>
<h6>Infected</h6>
<h5>{props.data["statewise"][0]["active"]}</h5>
<br />
<h6>No of Active Cases of Covid-19</h6>
<Container className="bg-primary " style={{height:"5px" ,width:"auto"}} height="20px" width="auto" >
</Container>
</Card.Body>
</Card></Col>
<Col xs={12} sm={12} md={4} className="justify-content-center ">
<Card style={{ width: '15rem' }} className="text-center ml-1 mr-1 mt-1 mb-1" >
<Card.Body>
<h6>Recovered</h6>
<h5>{props.data["statewise"][0]["recovered"]}</h5>
<br />
<h6>No of Recovered Cases of Covid-19</h6>
<Container className="bg-success " style={{height:"5px" ,width:"auto"}} height="20px" width="auto" >
</Container>
</Card.Body>
</Card></Col>
<Col xs={12} sm={12} md={4} className="justify-content-center ">
<Card style={{ width: '15rem' }} className="text-center ml-1 mr-1 mt-1 mb-1">
<Card.Body>
<h6>Deaths</h6>
<h5>{props.data["statewise"][0]["deaths"]}</h5>
<br />
<h6>No of Deaths Cases of Covid-19</h6>
<Container className="bg-dark " style={{height:"5px" ,width:"auto"}} height="20px" width="auto" >
</Container>
</Card.Body>
</Card>
</Col>
</Row>
</Container>
</>
);
}
https://i.stack.imgur.com/MqKYV.png
You just need to add class p-0(zero padding) to your Col and then on your Card element replace classes ml-1 mr-1 with just mx-auto (sets the margins on the x-axis, left & right to auto)
https://jsfiddle.net/85nj7tz1/
FYI .justify-content-center is used on a row to center the columns within the row.
The correct answer you already have with mx-auto.
On top of that you can also introduce h-100 on the cards to make them align properly on all screens. With a .card-footer you can align the border layout as well.
DEMO with columns
To take it a little further, you can get rid of the columns and use a card-layout.
A .card-deck seems the ideal approach.
DEMO with card-layout

How to automatically arrange cards in rows and columns?

Im trying to create a interactive user interface in ReactJS, using react-bootstrap. Im using cards and CardGroup. I need a way to automatically arrange the cards in new rows. Something like three cards per row, and if I have more than three, create a new row for the 4th card.
import React, { Component } from 'react';
import {Card,CardGroup} from 'react-bootstrap';
import {Button} from 'react-bootstrap';
const Items = ({items}) => {
return (
<>
<container>
<row>
<CardGroup>
{items.map((item) => (
<Card style={{ width: '18rem' }}>
<Card.Body>
<Card.Title>{item.NOME}</Card.Title>
<Card.Subtitle className="mb-2 text-muted">{item.CATEGORIA}</Card.Subtitle>
<Card.Text>
{item.DESCRICAO}
</Card.Text>
<Card.Link href="#">Card Link</Card.Link>
<Card.Link href="#">Another Link</Card.Link>
</Card.Body>
</Card>
))}
</CardGroup>
</row>
</container>
</>
)
};
export default Items
You could create an array with groups of 3 cards and then iterate over them:
const arr = var x = arr.reduce((item, key, index) => (index % 3 == 0 ? item.push([key]) : item[item.length-1].push(key)) && item, []);
const Items = ({items}) => (
<>
<container>
{arr.map(group => (
<row>
<CardGroup>
{group.map(card => (
<Card style={{ width: '18rem' }}>
<Card.Body>
<Card.Title>{item.NOME}</Card.Title>
<Card.Subtitle className="mb-2 text-muted">{item.CATEGORIA}</Card.Subtitle>
<Card.Text>{item.DESCRICAO}</Card.Text>
<Card.Link href="#">Card Link</Card.Link>
<Card.Link href="#">Another Link</Card.Link>
</Card.Body>
</Card>
))}
</CardGroup>
</row>
))}
</container>
</>
);
I haven't tested this code, but it should work...

Resources