align items next to each using bootstrap - reactjs

i have this images
i have loaded them from api and i created another component that will print them. my problem is that i have about 50 images and i want each 3 to be on one row with the title under them and the next one will hold 3 new but i don't really know bootstrap and can't seem to solve this problem.
postItem.js
import React from 'react';
function PostItem ({key,src,thumbnailUrl,onClick,title}) {
return (
<div>
<div key={key}>
<img src={src} thumbnailUrl={thumbnailUrl} onClick={onClick}></img>
<div>{title}</div>
</div>
</div>
)}
export default PostItem;
post.js:
<div><h3>Posts</h3></div>
<div>
<div>{newPhotosLocally.map(picture =>
<PostItem
key={picture.id}
src={picture.thumbnailUrl}
thumbnailUrl={picture.thumbnailUrl}
onClick={() => showPicture(picture.url,picture.id)}
title={picture.title}/>
// <div key={picture.id}>
// <img src={picture.thumbnailUrl} thumbnailUrl={picture.thumbnailUrl} onClick={() => showPicture(picture.url,picture.id)}></img>
// <div>{picture.title}</div>
// </div>
)}</div>
</div>

As you mentioned that you have already included bootstrap in your project, you can use col class to specify the width for each image.
You can read about grid system of bootstrap here: GRID SYSTEM
Your postItem.js file should look like this:
import React from 'react';
function PostItem ({key,src,thumbnailUrl,onClick,title}) {
return (
<div class="container-fluid text-center">
<div class="row">
<div class="col-4" key={key}>
<img src={src} thumbnailUrl={thumbnailUrl} onClick={onClick}></img>
<div>{title}</div>
</div>
<div class="col-4" key={key}>
<img src={src} thumbnailUrl={thumbnailUrl} onClick={onClick}></img>
<div>{title}</div>
</div>
<div class="col-4" key={key}>
<img src={src} thumbnailUrl={thumbnailUrl} onClick={onClick}></img>
<div>{title}</div>
</div>
</div>
</div>
)}

Related

Add Modal Component To Card component in React

I have created two different components, one for a card and one for a modal. What I would like to do now is to connect them together so that when I click the button on the card component, I would like the modal to open up on the screen. I have tried all that I can but can't seem to get it right.
Here is the card compenent:
import img from './112803.jpg';
import GitHub from './github-white.png';
import Website from './website-white.png';
import ModalButton from './ModalButton.js'
function Card(props) {
return (
<div className="card">
<div className="cardBody">
<div className="imageOverlay">
<img className="imageOverlayImg" src={img} alt="landscape" />
<div className="imageOverlayDescription">
<a href={props.gitHub} target="_blank" rel="noopener noreferrer">
<img src={GitHub} alt="GitHub Icon" />
</a>
<a href={props.liveSite} target="_blank" rel="noopener noreferrer">
<img src={Website} alt="liveSite Icon" />
</a>
</div>
</div>
<h2 className="cardTitle">{props.heading}</h2>
<p className="cardDescription">{props.description}</p>
</div>
<button className="cardButton">More Information</button>
</div>
);
}
export default Card;
And here is the modal component:
const Modal =(props)=>{
return(
<>
{props.showModal ? (
<div className="background">
<div className='modalWrapper' showModal={props.showModal}>
<img clasName="modalImg" src={require('./deals.jpg').default} alt="A Landscape" width="100%" height="100%"/>
<div className="modalContent">
<h1>{props.heading}</h1>
<p>{props.description}</p>
<p>{props.madeBy}</p>
<div className="buttonLayout">
<button className="innerModalButton">View Code</button>
<button className="innerModalButton">Live Site</button>
</div>
</div>
<button className="closeModalButton" aria-label="Close modal" onClick={()=>
props.setShowModal(prev => !prev)}>Close</button>
</div>
</div>
) : null}
</>
);
}
And here is some of the logic that the modal needs in order to pop up
const [showModal,setShowModal] = useState(false);
const openModal = () =>{
setShowModal(prev => !prev)
I am still a bit of a beginner and I'm not too sure how to get the project done. Any help would be greatly appreciated. Thank you.
Just add a state in Card pass pass it into Modal:
const [showModal,setShowModal] = useState(false);
...
<Modal
showModal={showModal}
setShowModal={setShowModal}
//...another props to display content
/>
From my understanding of what is going on, it looks like your modal button isn't doing anything. I would suggest you add an onClick method to it.
<button className="cardButton" onClick={()=> setShowModal(true)}> More Information</button>
Also like the answer above, pass the state from the card to the Modal
That could do it.
Let me know if it helped.

related to React js (props) css inline style

I want to more than one cards in different background color what i do ?
eg:
import React from "react";
import reactDom from "react-dom";
const BgColor=(props)=>{
return(
<>
<div className="main_div">
<div className="cards" >
<div className="card-side front" >
<div><span>C</span>opy<span>C</span>ode</div>
</div>
<div className="card-side back">
<div><i className="fas fa-copy fs-3" /><br />{props.code}</div>
</div>
</div>
</div>
</>
)
}
export default BgColor;
by pass value in app.js
and so on.....
please help me how to pass value inside the app.js
with this example is hard to to help you. Indeed, we don't know what your props object contains.
Let's assume your props contains
{
name: "toto",
code: "myCode"
}
In order to change background color based on props, you have multiples choices.
You can use dynamics classNames based on your props:
const BgColor=(props)=>{
return(
<>
<div className="main_div">
<div className="cards" >
<div className={`${props.name} card-side front`} >
<div><span>C</span>opy<span>C</span>ode</div>
</div>
<div className="card-side back">
<div><i className="fas fa-copy fs-3" /><br />{props.code}</div>
</div>
</div>
</div>
</>
)
}
See the <div className={${props.name} card-side front} > line. Go to css or scss file for this component and add a class selector inside of it.
In our example, it will be
.toto {
background: red
}
The second option is to inject directly the css inside your div.
Let's assume you send the desired background color inside your propos:
{
backgroundColor: "red",
code: "myCode"
}
In order to that, you have to do this
const BgColor=(props)=>{
return(
<>
<div className="main_div">
<div className="cards" >
<div className="card-side front" style={{backgroundColor: props.backgroundColor}}>
<div><span>C</span>opy<span>C</span>ode</div>
</div>
<div className="card-side back">
<div><i className="fas fa-copy fs-3" /><br />{props.code}</div>
</div>
</div>
</div>
</>
)
}
If you want to know more about css in React, you can check:
https://reactjs.org/docs/faq-styling.html, React.js inline style best practices, https://create-react-app.dev/docs/adding-a-stylesheet/
UPDATE
You can send an array of colors like <BgColor colors={["red", "blue"]} /> then in your BgColor component, you can map on your props like:
const BgColor=({colors})=>{
return (
<div className="main_div">
<div className="cards">
{colors.map((color, index) => {
return (
<div
className="card-side front"
style={{ backgroundColor: color }}
key={index}
>
<div>
<span>C</span>opy<span>C</span>ode
</div>
</div>
);
})}
</div>
</div>
);
}

Wrap ever 1 component into a div and display it in a grid column

Hello I'm kinda noob with react and I don't know how to slice every 1 item into a new div and then display it on the page with grid grid-cols-2 gap-4 tailwindcss styling, so I'd like to have 2 colums and 2 items in each row. Right now even I apply this tailwindcss there is 1 column and every 1 item is on a new row ;/ So I got to slice every one item.title, item.coverImage.url in a new div?
export default function BlogPage({ items }) {
console.log(items);
return (
<div className="max-w-lg mx-auto">
{items?.map((item) => (
<div key={item.slug} className="grid grid-cols-2 gap-4">
<Link href={`/portfolio/${item.slug}`}>
<div>
<a>
{item.title}
<Image
src={item.coverImage.url}
height={item.coverImage.height}
width={item.coverImage.width}
/>
</a>
</div>
</Link>
</div>
))}
</div>
);
}
You just need to add class col-span-1 to the div to explicitly tell how much column-span it needs to occupy.
export default function BlogPage({ items }) {
console.log(items);
return (
<div className="max-w-lg mx-auto">
{items?.map((item) => (
<div key={item.slug} className="grid grid-cols-2 gap-4">
<Link href={`/portfolio/${item.slug}`}>
<div className="col-span-1">
<a>
{item.title}
<Image
src={item.coverImage.url}
height={item.coverImage.height}
width={item.coverImage.width}
/>
</a>
</div>
</Link>
</div>
))}
</div>
);
}

How to make bootstrap cards be in the same line? Reactjs

I've been using Bootstrap 4 for displaying data to the user.
Now I've had idea to display data in columns and rows. Frontend is ReactJS and it's fetching data from backend response. Right now I've managed to display data in the cards, but they are all over the place.
This is how my dashboard looks:
So as can you see, it's all over the place, where I need to each one be in the same line. (they are not sticking with each other)
Is there a way to fix this with only Bootstrap or is it needed to create some kind of my own css?
Here is Dashboard.js component:
import React, { useState, useEffect } from 'react';
import ArticleService from '../services/article.service';
import { Link } from 'react-router-dom';
import Pagination from 'react-js-pagination';
const Dashboard = () => {
const [content, setContent] = useState([]);
const [currentPage, setCurrentPage] = useState(1);
const [postsPerPage] = useState(10);
useEffect(() => {
const fetchPosts = async () => {
const res = await ArticleService.articles();
setContent(res.data);
};
fetchPosts();
}, []);
// Get current posts
const indexOfLastPost = currentPage * postsPerPage;
const indexOfFirstPost = indexOfLastPost - postsPerPage;
const currentPosts = content.slice(indexOfFirstPost, indexOfLastPost);
// Change page
const handlePageChange = (pageNumber) => {
setCurrentPage(pageNumber);
};
return (
<div className='container'>
<div className='row'>
<div className='col-sm'>
<h4>Opis artikla</h4>
{currentPosts &&
currentPosts.map((item) => (
<div key={item.id} className='card'>
<h3>{item.descr}</h3>
<h5>Broj kvadrata: {item.sqm}</h5>
</div>
))}
</div>
<div classname='col-sm'>
<h4>Cijena</h4>
{currentPosts &&
currentPosts.map((item) => (
<div key={item.id} className='card'>
<h3>{item.price}</h3>
<h5>Cijena po kvadratu: {item.ppm2}/m2</h5>
</div>
))}
</div>
<div classname='col-sm'>
<h4>Prikaži ponudu</h4>
{currentPosts &&
currentPosts.map((item) => (
<div key={item.id} className='card'>
<Link to={`/article/${item.id}`}>
<h5>
Prikaži<br></br>
<br></br>
<br></br>
</h5>
</Link>
</div>
))}
</div>
</div>
<nav>
<div className='w3-bar w3-xlarge'>
<ul className='pagination justify-content-center'>
<li className='page-item'>
<Pagination
hideDisabled
hideNavigation
hideFirstLastPages
currentPage={currentPage}
itemsCountPerPage={10}
totalItemsCount={content.length}
pageRangeDisplayed={indexOfLastPost}
onChange={handlePageChange}
/>
</li>
</ul>
</div>
</nav>
</div>
);
};
export default Dashboard;
I think you can try using the bootstrap class - align-items-baseline on I guess the row div.
Check this answer from another post to know more about this align-items-baseline property.
If this does not work then I think you can try this:
<div className='container'>
<!-- First a row div with only the 3 column headings -->
<div className='row'>
<div className='col-4'>
<h4>Opis artikla</h4>
</div>
<div classname='col-4'>
<h4>Cijena</h4>
</div>
<div classname='col-4'>
<h4>Prikaži ponudu</h4>
</div>
</div>
<!-- Then the data of the 3 columns -->
{currentPosts && currentPosts.map((item) => (
<!-- A new row for each items data -->
<div className='row'>
<!-- For the first column -->
<div className='col-4'>
<div key={item.id} className='card'>
<h3>{item.descr}</h3>
<h5>Broj kvadrata: {item.sqm}</h5>
</div>
</div>
<!-- For the second column -->
<div className='col-4'>
<div key={item.id} className='card'>
<h3>{item.price}</h3>
<h5>Cijena po kvadratu: {item.ppm2}/m2</h5>
</div>
</div>
<!-- For the third column -->
<div className='col-4'>
<div key={item.id} className='card'>
<Link to={`/article/${item.id}`}>
<h5>
Prikaži<br></br>
<br></br>
<br></br>
</h5>
</Link>
</div>
</div>
</div>
))}
<nav>
<div className='w3-bar w3-xlarge'>
<ul className='pagination justify-content-center'>
<li className='page-item'>
<Pagination
hideDisabled
hideNavigation
hideFirstLastPages
currentPage={currentPage}
itemsCountPerPage={10}
totalItemsCount={content.length}
pageRangeDisplayed={indexOfLastPost}
onChange={handlePageChange}
/>
</li>
</ul>
</div>
</nav>
</div>
Update - I tried the above code and as you said, yes it didn't work.
But then I did the below code and it's working as you want it to or what I think you want it to work like:
import React from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
var currentPosts = [
{ sqm: 'One', ppm2: 'a', id: '1' },
{ sqm: 'Two', ppm2: 'b', id: '2' },
{ sqm: 'Three', ppm2: 'c', id: '3' }
];
const Dashboard = () => {
return (
<div className='container'>
<div className='row'>
<div className='col-4'>
<h4>Opis artikla</h4>
</div>
<div className='col-4'>
<h4>Cijena</h4>
</div>
<div className='col-4'>
<h4>Prikaži ponudu</h4>
</div>
</div>
{currentPosts && currentPosts.map((item) => (
<div className='row mt-4 align-items-baseline'>
<div className='col-4'>
<div key={item.id} className='card'>
<h3>{item.descr}</h3>
<h5>Broj kvadrata: {item.sqm}</h5>
</div>
</div>
<div className='col-4'>
<div key={item.id} className='card'>
<h3>{item.price}</h3>
<h5>Cijena po kvadratu: {item.ppm2}/m2</h5>
</div>
</div>
<div className='col-4'>
<div key={item.id} className='card'>
{/* <Link to={`/article/${item.id}`}> */}
<h5>
Prikaži<br></br>
<br></br>
<br></br>
</h5>
{/* </Link> */}
</div>
</div>
</div>
))}
</div>
);
};
export default Dashboard;
You can add the pagination and other code lines to this...
Actually the only change I made was to add the mt-4 and align-items-baseline classes to the row div that has the data.
The result I got from this code was:

React JS returning as HTML

I know that I am horrible at React JS but I couldn't find anything specific for this one
I want to return this function into my tsx file and write it on screen but it should be in a component
function getUsers() {
const Users = ({ users } : { users:any }) => {
return (
<div>
{users.map((user : any, i : any) => (
<div className="card">
<img className="card-img-top" src={userPic} alt="Card image cap" />
<div className="card-body">
<h5 className="card-title">{user.name}</h5>
<h6 className="card-subtitle mb-2 text-muted">{user.email}</h6>
</div>
</div>
))}
</div>
)
};
return Users;
}```
ReactDOMServer has a function called renderToStaticMarkup that can take some JSX and return the static HTML as a string.
The issue you'll likely have is it isn't formatted with indentation etc.
Your function will probably look something like this:
const generateHtml = (users: any[]) => {
const html = (
<div>
{users.map((user: any, i: any) => (
<div className="card" key={i.toString()}>
<img className="card-img-top" src={user.userPic} alt="Card image cap" />
<div className="card-body">
<h5 className="card-title">{user.name}</h5>
<h6 className="card-subtitle mb-2 text-muted">{user.email}</h6>
</div>
</div>
))}
</div>
);
return ReactDOMServer.renderToStaticMarkup(html);
};
The resulting HTML is:
<div><div class="card"><img class="card-img-top" src="1.png" alt="Card image cap"/><div class="card-body"><h5 class="card-title"></h5><h6 class="card-subtitle mb-2 text-muted"></h6></div></div></div>

Resources