How to create a dynamic grid in react.js? - reactjs

I'm trying to make a responsive grid system in react.js
I want to show 3 cards in a row. Each card consists of 4 columns.
But I want to make only 1 component which will render my cards and show them on screen. But i'm unable to make the logic of dynamically showing these cards in a row as a row contains only 3 cards. After that next row will start
Here is my card Component code
import {
Card, CardImg, CardText, CardBody,
CardTitle, CardSubtitle, Button, Container, Row, Col
} from 'reactstrap';
const INNERCARD = (props) => {
return (
<Container>
<Row>
<Col md={3}>
<Card>
<CardImg top width="100%" src="/assets/318x180.svg" alt="Card image cap"/>
<CardBody>
<CardTitle tag="h5">Card title</CardTitle>
<CardSubtitle tag="h6" className="mb-2 text-muted">Card subtitle</CardSubtitle>
<CardText>Some quick example text to build on the card title and make up the bulk of the
card's
content.</CardText>
<Button>Button</Button>
</CardBody>
</Card>
</Col>
</Row>
</Container>
);
};
export default INNERCARD;```

I got this working with a little bit of tinkering. Never used reactstrap before, so it was fun to play around with it a bit. First you need to also install bootstrap into your project, not sure if you knew that or not. Then you need to include the bootstrap css file in your index.js file.
So I am using a freshly installed CRA app. The top of my index.js looks like this:
import React from 'react';
import ReactDOM from 'react-dom';
import 'bootstrap/dist/css/bootstrap.min.css';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
So once you have Bootstrap installed and imported it, then you can play around with reactstrap, assuming that is also installed. I learned from the docs that you want <Row xs={3}> if you want 3 cards per row.
So here is my full App.js file that works with any number of cards and shows 3 per row:
import './App.css';
import {
Card, CardImg, CardText, CardBody,
CardTitle, CardSubtitle, Button, Container, Row, Col
} from 'reactstrap';
function App() {
// obviously I am just hard-coding in a random number here. This is where
// you would have a call to an API or something where you would get all
// your card data from, ideally that API call would return an array of data
// and then you could just map over that array to output the card data
// down below in the render section
const numberOfCards = 11;
return (
<div className="App">
<Container>
<Row xs={3}>
{[...Array(numberOfCards)].map((e, i) => {
return (
<Col>
<Card>
<CardImg top width="100%" src="/assets/318x180.svg" alt="Card image cap"/>
<CardBody>
<CardTitle tag="h5">Card title #{i+1}</CardTitle>
<CardSubtitle tag="h6" className="mb-2 text-muted">Card subtitle</CardSubtitle>
<CardText>Some quick example text to build on the card title and make up the bulk of the
card's
content.</CardText>
<Button>Button</Button>
</CardBody>
</Card>
</Col>
)
})}
</Row>
</Container>
</div>
);
}
export default App;
Forgot to include what it looks like! Here it is!

//Mapping over the Bootstrap Card and then displaying them in the Grid formate
import React from 'react'
import { Col, Container, Row } from 'react-bootstrap';
// Array of Data
const StudentData = [
{
Name: "Waji",
Degree: "Bachlorrs"
},
{
Name: "Zain",
Degree: "Matric"
},
{
Name: "Rahat",
Degree: "B.Com"
},
{
Name: "Wajahat",
Degree: "Bachlorrs"
},
{
Name: "Sabi",
Degree: "Bachlorrs"
}
]
let NewCard = () => {
return (
<Row>
{StudentData.map((props) => {
return (
<Col sm={6} md={4} className='mt-3'>
<div className="card" style={{ width: '18rem' }}>
<div className="card-body">
<h5 className="h5">{props.Name}</h5>
<p className="card-text">{props.Degree}</p>
Click
</div>
</div>
</Col>
)
})}
</Row>
)
}
export default function MyCard() {
return (
<Container>
<NewCard
title=''
calss=''
/>
</Container>
)
}
ReactDOM.render(
<MyCard />,
document.getElementById('root')
)
Creates the following:

Related

horizontal centering with React Bootstrap

Checked the docs and my code still won't center horizontally.
import React from 'react';
import '../css/Header.css';
import Container from 'react-bootstrap/Container';
import Col from 'react-bootstrap/Col';
import Row from 'react-bootstrap/Row';
const Header = () => {
return (
<header className='d-flex justify-content-center header' style={{ backgroundImage: `url("/images/header.png")` }}>
<Container fluid>
<Row>
<Col>
<div className={'website-title'}>
My Website
<div className={'bracket bracket-1'}></div>
<div className={'bracket bracket-2'}></div>
<div className={'bracket bracket-3'}></div>
<div className={'bracket bracket-4'}></div>
</div>
</Col>
</Row>
</Container>
</header>
)
}
export default Header;
I'm using the "d-flex justify-content-center" class like the docs suggest. Any ideas what i'm doing wrong here?
You can try to place the <header> inside the <Container>
Put it in a div and the side margins of the header should center in it

I am trying to use componentdidmount function

I am new to react,
I am learning react, In this process I tried to create a for componentdidmount forreact. Were I am stuck in the following code
Below is my code
import React from "react";
import {Card, Button, Container, Row, Col} from 'react-bootstrap';
import addquantity from '../Functions/quantityadd.js';
const numberOfCards = 100;
const Home = (props) => {
componentdidMount(){
console.log('First this called');
}
return (
<div>
<h2>Home</h2>
<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">Test</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)}>Button</Button>
</Card.Body>
</Card>
</Col>
)
})}
</Row>
</Container>
</div>
);
}
export default Home;
I need to load the data of the product list. so I am using componentdidmount
Can you help me to understand what I am doing wrong here.
Thanks in advance
first of all this is "componentDidMount"
And in functional components we use "useEffect" for life cycle hooks and
"componentDidMount" is for class components

CardImgOverlay in Reactstrap

I'm working with the Card component of Reactstrap in React.
So what I need is a given image as the background for the card heading and card title. So I refered the documentation and used CardImgOverlay component
Given below is the code snippet
import React, { Component } from 'react'
import { Form, Card, CardTitle, CardText, CardImg, CardImgOverlay, Container, Row, Col } from 'reactstrap'
import './style.css'
class WeatherComponent extends Component{
render(){
return(
<div>
<Container>
<Row>
<Col>
<Card>
<CardImg width="50%" src="assets/images/sunny.jpg" alt="Card image cap" />
<CardImgOverlay>
<CardTitle className='text'>Weather</CardTitle>
<CardText className='text'>Summer</CardText>
</CardImgOverlay>
</Card>
</Col>
</Row>
</Container>
</div>
)}
}
export default WeatherComponent
But the above page is not rendering correctly and what I'm getting is this:
A solution to fix this problem is much appreciated.
import 'bootstrap/dist/css/bootstrap.min.css';

make react bootstrap column md

I'm learning react and react-bootstrap. I want to make a simple application such as a card (using react-bootstrap) and display it with col md = 3 so that the card becomes 4 in 1 row. UI from youtube can be example, we can consider the example from the youtube UI as a card. Every 1 row I want 4 cards like the Youtube UI image that I gave. How can I do that?
Image of my app that i made so far.
App.js:
import React from "react";
import "./App.css";
import { Container, Row } from "react-bootstrap";
import Col3 from "./components/col3.component";
class App extends React.Component {
constructor() {
super();
this.state = {
payments: []
};
}
async componentDidMount() {
const responsePayments = await fetch(
"https://api.bukalapak.com/v2/products/f3vi.json"
);
const paymentJson = await responsePayments.json();
this.setState({ payments: paymentJson.product.installment });
}
render() {
return (
<Container>
<Row>
<Col3 payments={this.state.payments}/>
{console.log(this.state.payments)}
</Row>
</Container>
);
}
}
export default App;
col3.component.jsx:
import React from "react";
import {
Card,
Button
} from "react-bootstrap";
const Col3 = props => {
return (
<div>
{props.payments.map(payment => {
return (
<Card key={payment.bank_issuer} style={{ width: '18rem' }}>
<Card.Img variant="top" src={payment.url_logo} />
<Card.Body>
<Card.Title>Card Title</Card.Title>
<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 variant="primary">{payment.bank_issuer}</Button>
</Card.Body>
</Card>
);
})}
</div>
);
};
export default Col3;
Notes: I am using this API in product.installment object (line 157) so that can get 17 banks sample.
Inside the Row you are directly putting your custom component,
<Row>
<Col3 payments={this.state.payments}/>
{console.log(this.state.payments)}
</Row>
And in child component giving style={{ width: '18rem' }} to the element will not set it be as Col.
You actually need to write the Col.
import { Card, Button, Col} from "react-bootstrap";//import Col here
const Col3 = props => {
return (
<> //Use Fragment instead of div
{props.payments.map(payment => {
return (
<Col md={3}> //Col start
<Card key={payment.bank_issuer} style={{ width: '18rem' }}> //You may not need this style now
<Card.Img variant="top" src={payment.url_logo} />
<Card.Body>
<Card.Title>Card Title</Card.Title>
<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 variant="primary">{payment.bank_issuer}</Button>
</Card.Body>
</Card>
</Col> //Col end
);
})}
</>
);
};
For more about Grid check the docs

Align Cards horizontally using ReactStrap

I am trying to create a deck of cards using reactstrap. I want the cards to be aligned horizontally but the cards are aligned vertically. Can someone please tell me how achieve this? Thanks
These are the two Components
import React, { Component } from "react";
import {
Card,
CardImg,
CardText,
CardBody,
CardTitle,
CardSubtitle,
Button,
Col
} from "reactstrap";`
class MovieItem extends Component {
render() {
return (
<Card>
<CardTitle>Title</CardTitle>
</Card>
);
}
}
export default MovieItem;`
MovieList.js
import { Row, Col, CardDeck, CardGroup, Container } from "reactstrap";
import React, { Component } from "react";`
import MovieItem from "./MovieItem";`
class MoviesList extends Component {
render() {
let movies;
if (this.props.movies) {
movies = this.props.movies.map(movie => {
return <MovieItem key={movie.id} movie={movie} />;
});
}
return (
<Container fluid>
<CardDeck> {movies}</CardDeck>
</Container>
);
}
}
export default MoviesList;
As Bootstrap and Reactstrap Card said, you can integrate them like this :
<Row>
<Col>
<Card>
<Row className="no-gutters">
<Col md="4">
<CardImg
top
width="100%"
src="https://placeholdit.imgix.net/~text?txtsize=33&txt=318%C3%97180&w=318&h=180"
alt="Card image cap"
/>
</Col>
<Col md="8">
<CardBody>
<CardTitle>Card title</CardTitle>
<CardSubtitle>Card subtitle</CardSubtitle>
<CardText>
Some quick example text to build on the card title and
make up the bulk of the card's content.
</CardText>
<Button>Button</Button>
</CardBody>
</Col>
</Row>
</Card>
</Col>
</Row>
I have encountered the same problem. The basic solution is to put your group of cards inside a Row Component. This is a snippet from my code.
<Container fluid>
<Row>
<ClassroomCard/>
<ClassroomCard/>
<ClassroomCard/>
</Row>
<Row>
<ClassroomCard/>
<ClassroomCard/>
<ClassroomCard/>
</Row>
</Container>
Later, it's up to you to change the style of your elements accordingly. I hope this will help those who will encounter this problem in the future.

Resources