make react bootstrap column md - reactjs

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

Related

React duplicate component getting displayed using layout

I'm having trouble with React. I've read the documentation but I'm still confused as to what steps need to be taken.
I have created PrimaryLayout and using props children items is called. In the index file(Page) the PrimaryLayout is called and embedded card component.
However, in the output I am seeing duplicate components.
Appreciate your help.
Please find below code
PrimaryLayout.js
import React from 'react'
import Header from "../components/Header"
import FooterMessage from "../components/FooterMessage"
const PrimaryLayout = (props) => (
<div>
<Header/>
<div className="container">
<div className="row justify-content-md-center">
<div className="col-xs-6">
{props.children}
</div>
</div>
</div>
<FooterMessage/>
</div>
);
export default PrimaryLayout;
Card Component
import React from 'react'
import {Card, Button} from 'react-bootstrap'
const CardBox = (props) => (
<div className ="p-3">
<Card>
<Card.Img variant="top" src="https://loremflickr.com/640/360" />
<Card.Body>
<Card.Title>{props.titleFirst}</Card.Title>
<Card.Text>{props.description}</Card.Text>
<Button variant="primary">Go somewhere</Button>
</Card.Body>
</Card>
<Card>
<Card.Img variant="top" src="https://loremflickr.com/640/360" />
<Card.Body>
<Card.Title>{props.titleSecond}</Card.Title>
<Card.Text>{props.description}</Card.Text>
<Button variant="primary">Go somewhere</Button>
</Card.Body>
</Card>
</div>
);
export default CardBox;
Index.js
import React from "react"
import CardBox from "../components/CardBox"
import PrimaryLayout from "../layouts/PrimaryLayout"
export default () => (
<PrimaryLayout>
<CardBox
titleFirst="This is my first post"
description = "We are writing something to be displayed in description"
/>
<CardBox
titleSecond="This is my first post"
description = "We are writing something to be displayed in description"
/>
</PrimaryLayout>
)
If I understand it correctly, this has to do with the CardBox component. Right now you create 2 Card components within the CardBox component. This means that for every CardBox component you'll get 2 Card components. The first Card with titleFirst and the description, the second Card with titleSecond and the description.
If you change the BoxCard component to display the Card only ones like so:
const CardBox = (props) => (
<div className ="p-3">
<Card>
<Card.Img variant="top" src="https://loremflickr.com/640/360" />
<Card.Body>
<Card.Title>{props.title}</Card.Title>
<Card.Text>{props.description}</Card.Text>
<Button variant="primary">Go somewhere</Button>
</Card.Body>
</Card>
</div>
);
and let the index.js use it like so:
export default () => (
<PrimaryLayout>
<CardBox
title="This is my first post"
description = "We are writing something to be displayed in description"
/>
<CardBox
title="This is my first post - second CardBox"
description = "We are writing something to be displayed in description"
/>
</PrimaryLayout>
);
Hope this is what you are looking for!

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

How to create a dynamic grid in react.js?

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:

How do I use a variable to return a named import in React?

I want to be able to have the value of a variable (generated by a map function) be used as a named object in curly braces. I'm kind of new to React and ES6 (been teaching myself through a pet project), so I may not be asking the question properly.
I'm utilizing an NPM package (react-icons-kit) that allows me to import font icons as React modules, that then are rendered as SVGs. I have a JSON file with objects (each object has: id, name, category, description, icon) and each object has a related icon assigned to it.
When I'm rendering an module, I have to specify the icon I want to use with curly braces. I am using a map function to process the JSON data to display each object nicely, and want to be able to dynamically create an icon using the map variable {obj.icon}.
import Icon from 'react-icons-kit';
import { font, clock0, html5 } from 'react-icons-kit/fa';
...
// Sample data
array = [...{"name":"Some Name","icon":"font"},{"name":"Some
Othername","icon":"html5"},{"name":"Another Name","icon":"clock0"}...]
...
// Should render an SVG icon when use with other code
var foo = array.map((obj) => {
console.log(obj.icon) // correctly logs font html5 clock0
return(
{obj.icon} // prints the value of the var fine (font html5 clock0)
<Icon icon={clock0}/> // manually specifying works
<Icon icon={obj.icon}/> // using a variable gives error
);
});
...
/*================
FULL CODE (SORRY)
=================*/
import React, { Component } from 'react';
import ScrollableAnchor from 'react-scrollable-anchor'
import { TabContent, TabPane, Nav, NavItem, NavLink, Row, Col } from 'reactstrap';
import { Media } from 'reactstrap';
import Icon from 'react-icons-kit';
import { font, clockO, html5 } from 'react-icons-kit/fa';
class Skills extends Component {
constructor(props) {
super(props);
this.state = {
activeTab: 'Coding',
};
this.toggle = this.toggle.bind(this);
}
toggle(tab) {
if (this.state.activeTab !== tab) {
this.setState({
activeTab: tab
});
}
}
render() {
// Skill Navigation
let SkillNav = () => {
let SkillNavItems = this.props.categories.map((category) => {
let active = (this.state.activeTab === category) ? "active" : "";
return (
<NavItem>
<NavLink
className={active}
onClick={() => { this.toggle(category); }}>
{category}
</NavLink>
</NavItem>
);
});
return (
<Nav pills vertical>
{SkillNavItems}
</Nav>
);
}
// Different tabs of Skills
var SkillTabs = () => {
var SkillTab = this.props.categories.map((category) => {
// Separate objects by the category supplied to it
var SkillTabCategory = this.props.skills.filter(function (skill) {
return skill.category === category;
});
// Display a list of objects with the same categories
var SkillTabContent = SkillTabCategory.map((skill) => {
return (
<Media list>
<Media tag="li">
<Media className="mr-5">
<Icon icon={skill.icon} />
</Media>
<Media body>
<Media heading>
{skill.name}
</Media>
{skill.description}
</Media>
</Media>
</Media>
);
});
return (
<TabPane tabId={category}>
<Row>
<Col sm="12">
<h4>{category}</h4>
{SkillTabContent}
</Col>
</Row>
</TabPane>
);
});
return (
<TabContent activeTab={this.state.activeTab}>
{SkillTab}
</TabContent>
);
}
return (
// Putting it all together
<ScrollableAnchor id='skills'>
<div className="wrapper">
<div className="container">
<Row className="vh100 align-items-center">
<Col xs="4" sm="4">
<SkillNav></SkillNav>
</Col>
<Col xs="12" sm="8">
<SkillTabs></SkillTabs>
</Col>
</Row>
</div>
</div>
</ScrollableAnchor>
);
}
}
export default Skills;
If your icons names come from your json means dynamically then you need to first import all the icons from react-icons-kit like .
import * as icons from 'react-icons-kit/fa';
Now you can uses icons to fetch the icons runtime like
const array = [{"name":"Some Othername","icon":"html5"}]
export default ({ name }) => (
<>
{array.map((item)=>{
return(
<Icon icon={icons[item.icon]} />
)
})}
</>
);
check running example here Demo
Codesandbox
Initialize the named-import inside the constructor function with this.font=font;this.html5=html5;.
Then render it with <Icon icon={this[obj.icon]} />

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