How to pass parameter value into modal box with react - reactjs

I am still not familiar with react.js and I am doing a NFT app whereby you click a button and each individual NFT profile will show in a popup modal box which I have already successful integrate the modal box and able to pass the value to a function handleShow(owner) but I have no idea how to send or display this value (owner) into my modal box. Appreciate you guys, can help on my situation. thank you.
const [show, setShow] = useState(false);
const handleClose = () => setShow(false);
function handleShow(owner) {
alert(owner);
setShow(true);
}
<div className="min-h-screen bg-gray-100">
<div className="max-w-7xl mx-auto sm:px-6 lg:px-8 ">
<div className="text-black-100 text-6xl pt-28 pb-10">Monkey</div>
{mintedNftState.state === "PENDING" && (
<div className="text-xl text-white">LOADING...</div>
)}
{mintedNftState.state === "SUCCESS" && (
<div className="grid grid-cols-3 gap-4">
{mintedNftState.data.map(
({ id, image, name, description, owner }) => {
return (
<div key={id} className="bg-white rounded p-2">
<img src={image} className="mx-auto p-4" alt={name} />
<div className="text-xl">{name}</div>
<div className="font-myfont">{description}</div>
<hr className="my-4" />
<div className="text-left text-sm">Owned By:</div>
<div className="text-left text-xs">{owner}</div>
<div className="mt-12">
<Button variant="primary" onClick={() => handleShow(owner)}>
Launch demo modal
</Button>
</div>
</div>
);
}
)}
</div>
)}
</div>
<Modal show={show} onHide={handleClose}>
<Modal.Header closeButton>
<Modal.Title>Modal heading</Modal.Title>
</Modal.Header>
<Modal.Body>
</Modal.Body>
<Modal.Footer>
<Button variant="secondary" onClick={handleClose}>
close
</Button>
<Button variant="primary" onClick={handleClose}>
Save Changes
</Button>
</Modal.Footer>
</Modal>
</div>

I would first add an owner state
const [owner, setOwner] = useState(null);
In handleShow you can set the owner like this
function handleShow(owner) {
setOwner(owner)
setShow(true);
}
You can finally display the owner value between <Modal.Body> tags
<Modal.Body>
<p>{owner}</p>
</Modal.Body>
When closing the modal you can set the owner to null
const handleClose = () => {
setShow(false);
setOwner(null);
}

Related

Compiled with problems: ERROR in ./node_modules/#popperjs/core/lib/popper-base.js 3:0-57

Compiled with problems:
ERROR in ./node_modules/#popperjs/core/lib/popper-base.js 3:0-57
export 'detectOverflow' (reexported as 'detectOverflow') was not found in './createPopper.js' (possible exports: createPopper, popperGenerator)
import React, {useState} from "react";
import img1 from '../src/images/card-img1.webp'
import {Button, Modal} from 'react-bootstrap';
const Cards = () => {
const [show, setShow] = useState(false)
const closemodal =()=> setShow(false)
const openmodal =()=> setShow(true)
return (
<>
<section className="pt-4 pt-md-11">
<div className="container">
<div className="row align-items-center">
<div className="card mt-4 mb-4" style={{ width: "18rem", }}>
<img className="card-img-top w-100 p-3" src={img1} alt="Card" />
<div className="card-body">
<h5 className="card-title">Card title</h5>
<p className="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<Button variant="primary" onClick={openmodal}>
Watch
</Button>
</div>
</div>
</div>
</div>
</section>
<Modal
show={show}
onHide={closemodal}
backdrop="static"
keyboard={false}
>
<Modal.Header closeButton>
<Modal.Title>Modal title</Modal.Title>
</Modal.Header>
<Modal.Body>
I will not close if you click outside me. Don't even try to press
escape key.
</Modal.Body>
<Modal.Footer>
<Button variant="secondary" onClick={closemodal}>
Close
</Button>
<Button variant="primary">Understood</Button>
</Modal.Footer>
</Modal>
</>
)
}
export default Cards;

react bootstrap offcanvas custom close button

I got offcanvas working on react using bootstrap. Now I want add a custom close button but it doesn't seems to work. Please guide me on what I'm doing wrong.
<Offcanvas
show={show}
placement="bottom"
onHide={handleClose}
{...props}
className={css["offcanvas-bottom"]}
>
<Offcanvas.Header className="p-0">
<button type="button" class="btn-close text-reset new_close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
<div className={css["pop-image"]}>
<Image
src="https://xxxxxxx/2022030117344459.jpg"
fluid
/>
</div>
</Offcanvas.Header>
<Offcanvas.Body>
Some text as placeholder. In real life you can have the elements you
have chosen. Like, text, images, lists, etc.
</Offcanvas.Body>
</Offcanvas>
I used onClick in my custom button. And toggled the state by doing
const toggleOffcanvas = () => setShow(!show)
Example:
function ToggleSidebarOffcanvas(){
const [show, setShow] = useState(false);
const toggleOffcanvas = () => {
setShow(!show);
};
return (
<Button onClick={toggleOffcanvas}>
Menu
<SidebarOffcanvas show={show} toggleOffcanvas={toggleOffcanvas} />
</Button>)
}
function SidebarOffcanvas({ show, toggleOffcanvas }) {
return (
<Offcanvas className="w-25" show={show} scroll={true} backdrop={false}>
<Offcanvas.Header
className="p-0"
style={{
backgroundColor: "#008069",
color: "white",
}}
>
<Offcanvas.Title>
<div
className="d-flex align-items-end w-100 mb-2 lh-1"
>
<div className="p-2" onClick={toggleOffcanvas}>
<FiArrowLeft />
</div>
<h5 className="ms-3">Profile </h5>
</div>
</Offcanvas.Title>
</Offcanvas.Header>
<Offcanvas.Body>
<div >
...
</div>
</Offcanvas.Body>
</Offcanvas>
);
}

How can I display my data dynamically onclick event in React

I'm creating a movies app with React and Redux, in each movie card I have some information about the movie like the title, image, and a button(buy a ticket). The idea is when I click on the button of each card I want to get the same image and title of the card and display it on the same page on another card that going to pop up so the user can choose the quantity and continue.
How can I get the data from the movie card onclick and transform it to another card as a pop-up?
what do you think
Single movie card Component
const SingleMovieCard = ({ id, title, poster_path, overview, toggleHandler }) => {
const [selected, isSelected] = useState(null);
return (
<article key={id} className="card">
<div key={id} onMouseEnter={() => isSelected(id)} onMouseLeave={() => isSelected(null)}>
<img src={`${ImgPath}` + poster_path} alt={title} className="image" />
{selected === id && <video src="./Trailers/SpaceJam.mp4" autoPlay={true} loop muted />}
</div>
<div className="body-card">
<h1>{title}</h1>
<p>{`${overview.substring(0, 200)}...`}</p>
</div>
<div className="services">
<FiShare2 className="icon" />
<FiHeart className="icon" />
<div className="btn-icon-container">
<BiShoppingBag className="btn-icon" />
<button onClick={() => toggleHandler()}>Buy Ticket</button>
</div>
</div>
</article>
)
}
export default SingleMovieCard;
Pop-up movie card
const PopUpMovie = ({showClass, toggleHandler}) => {
const moviesList = useSelector((state)=> state.allMovies.movies);
return (
<div className={`pop-up-container ${showClass}`}>
<nav className="pop-up">
<GrClose className="pop-up-close" onClick={()=> toggleHandler()}/>
<div className="product-details">
<div className="img-container">
<img src="./Pictures/FreeGuy.jpg" alt="FreeGuy" />
</div>
<div className="product info">
<h1 className="title">Free Guy movie</h1>
<div className="quantity">
<h4>Quantity</h4>
<span>4</span>
</div>
<h5 className="prix">11$</h5>
<button className="btn-checkout">Continue to checkout</button>
</div>
</div>
</nav>}
</div>
)
}
export default PopUpMovie;
you can use Modal from react-bootstrap
Example:
import { Modal } from "react-bootstrap";
const PopUpMovie = ({ showClass, toggleHandler }) => {
const modalContent = (
<div className={`pop-up-container ${showClass}`}>
<nav className="pop-up">
<GrClose className="pop-up-close" onClick={() => toggleHandler()} />
<div className="product-details">
<div className="img-container">
<img src="./Pictures/FreeGuy.jpg" alt="FreeGuy" />
</div>
<div className="product info">
<h1 className="title">Free Guy movie</h1>
<div className="quantity">
<h4>Quantity</h4>
<span>4</span>
</div>
<h5 className="prix">11$</h5>
<button className="btn-checkout">Continue to checkout</button>
</div>
</div>
</nav>
</div>
)
const moviesList = useSelector((state) => state.allMovies.movies);
return (
<Modal
id="order-modal-close"
backdrop="static"
show={showClass}
size={"md"}
dialogClassName="modal-90w"
onHide={toggleHandler}
>
<Modal.Header closeButton>
<Modal.Title>Movie</Modal.Title>
</Modal.Header>
<Modal.Body >{modalContent}</Modal.Body>
{modalFooter}
</Modal>
)
}

React Js Multiple Modal

Hi i'm trying to learn react and i was tryign to make multiple modal but the same desgine and everything but different content inside it how?
import './Works.css';
import React, { useState } from 'react'
import Modal from '../Modal/Modal'
function Works(props){
const [isOpen, setIsOpen] = useState(false)
return(
<div>
<div className={props.NameClass}>
<h1>{props.icon}</h1>
<h1>{props.title}</h1>
<p >simple pargraph</p>
<button className="button_more" onClick={() => setIsOpen(true)}><i className="fas fa-caret-right"></i></button>
</div>
<Modal open={isOpen} title="hi" onClose={() => setIsOpen(false)}>
</Modal>
</div>
)
}
export default Works
and if you look at <Modal open={isOpen} title="here i wanna different title" onClose={() => setIsOpen(false)}>
and here the App.js calling the file on top multiple times but i wanna for example if someone click on the second project it change the title to for example "hi you're in the second modal!"
<div class="Div-Projects">
<Works NameClass="First_Project" icon="💳" title="first" />
<Works NameClass="Second_Project" icon="🎓" title="second" />
<Works NameClass="Third_Project" icon="👩🏻‍💻" title="third" />
</div>
and here the modal code!
import './Modal.css'
function Modal({open , title, onClose}){
if(!open) return null
return(
<div className="popup">
<div className="content">
<h1>{title}</h1>
<div className="p">
<p>login system for students attendees with an arduino and python it shows how fast and stable for doing the job.login system for students attendees with an arduino and python it shows how fast and stable for doing the job.login system for students attendees with an arduino and python it shows how fast and stable for doing the job.</p>
</div>
<div className="buttons">
<button className="dismiss" onClick={onClose}>Dismiss!</button>
<button className="github" onClick={() => window.open( 'https://github.com/Majiedo/Login_System')}><i className="fab fa-github"></i></button>
<button className="code"><i className="fas fa-code"></i></button>
</div>
</div>
</div>
)
}
export default Modal
Why don't you pass Works props to Modal component? Like this:
function Works(props){
const [isOpen, setIsOpen] = useState(false)
return(
<div>
<div className={props.NameClass}>
<h1>{props.icon}</h1>
<h1>{props.title}</h1>
<p >simple pargraph</p>
<button className="button_more" onClick={() => setIsOpen(true)}><i className="fas fa-caret-right"></i></button>
</div>
<Modal open={isOpen} title={`hi you're in the ${props.title} modal!`} onClose={() => setIsOpen(false)}/>
</div>
)
}
You need to pass in title {props.title}
change it:
<Modal open={isOpen} title="hi" onClose={() => setIsOpen(false)}>
</Modal>
On this:
<Modal open={isOpen} title={`hi you're in the ${props.title}`} onClose={() => setIsOpen(false)}/>

Ternary expression does not update the jsx template

I'm using react-bootstrap-table-2 to build a table where I want the user to be able to change columns in a modal UI.
When a column is removed by the user the change should not take effect until the modal is closed, so until then I want the UI to reflect that the column will be removed by a strike-through of the row. So I have a ternary expression on col.deleted that shows the text with strike through if true.
I map over the columns array to create new rows for each column, and a button to remove the column that gets the index of the column.
<tbody>
{columns.map((col: any, i: number) => (
<tr key={i}>
<td>{col.deleted ? <del>{col.text}</del> : col.text}</td>
<td className="w-20">
<div className="flex flex-row justify-center">
<Button
variant="danger"
onClick={() => {
col.deleted = !col.deleted;
setColumns(columns);
}}
>
<FaTrash />
</Button>
</div>
</td>
</tr>
))}
</tbody>
I set columns[i].deleted to true with the button but nothing gets updated in the UI. Have I misunderstood something?
Check this sandbox for the full example: https://codesandbox.io/s/silly-wilbur-q3ytt
Likely the issue is that you are mutating the state. Try let newColums = [...columns]; and then perform the same things but on newColumns not columns, and set newColumns in setState
I had same issue recently with a simper example and it came down to that I was mutating the state and thus the component setState was not updating the component
EDIT example
// Your onClick function
(i) => {
let newColumns = [...columns];
newColumns[i].deleted = !newColumns[i].deleted;
setState(newColumns);
}
Your function should look somewhat like this just adjust the code in case I made typo
Sandbox tested
import React, { useState } from "react";
import Button from "react-bootstrap/Button";
import Modal from "react-bootstrap/Modal";
import Table from "react-bootstrap/Table";
// #ts-ignore
import cellEditFactory from "react-bootstrap-table2-editor";
// #ts-ignore
import BootstrapTable from "react-bootstrap-table-next";
import { FaTrash, FaEdit, FaPlus } from "react-icons/fa";
import { rows, columns as cols } from "./data";
import "react-bootstrap-table-next/dist/react-bootstrap-table2.min.css";
import "bootstrap/dist/css/bootstrap.min.css";
export interface ColumnItem {
dataField: string;
text: string;
deleted: boolean;
}
const EditTableModal = (props: any) => {
const [columns, setColumns] = useState<ColumnItem[]>(
props.columns.map((col: any) => {
col.deleted = false;
return col;
})
);
const handleClick = i => {
let newColumns = [];
columns.forEach(column => newColumns.push(Object.assign({}, column)));
console.log(newColumns);
console.log(i);
newColumns[i].deleted = !newColumns[i].deleted;
console.log(newColumns[i].deleted);
setColumns(newColumns);
};
return (
<Modal
{...props}
size="lg"
aria-labelledby="contained-modal-title-vcenter"
centered
>
<Modal.Header closeButton>
<Modal.Title id="contained-modal-title-vcenter">
<div className="flex flex-row items-center">
<FaEdit className="mr-2" /> Edit columns
</div>
</Modal.Title>
</Modal.Header>
<Modal.Body>
<Table striped bordered hover size="sm" className="mb-0">
<tbody>
{columns.map((col: any, i: number) => (
<tr key={i}>
<td>{col.deleted ? <del>{col.text}</del> : col.text}</td>
<td className="w-20">
<div className="flex flex-row justify-center">
<Button variant="danger" onClick={() => handleClick(i)}>
<FaTrash />
</Button>
</div>
</td>
</tr>
))}
<tr>
<td>
<div>
<Button variant="dark">
<FaPlus />
</Button>
</div>
</td>
<td className="w-20" />
</tr>
</tbody>
</Table>
</Modal.Body>
<Modal.Footer>
<Button variant="dark" onClick={props.onHide}>
Save
</Button>
<Button variant="light" onClick={props.onHide}>
Close
</Button>
</Modal.Footer>
</Modal>
);
};
const ComponentList = () => {
const [columns] = useState<ColumnItem[]>(cols);
const [data] = useState<any[]>(rows);
const [modalShow, setModalShow] = useState(false);
return (
<div className="flex flex-col items-center m-3">
{data.length > 0 ? (
<div className="flex flex-col items-center">
<div className="flex flex-row m-2">
<Button
variant="dark"
onClick={() => setModalShow(true)}
className="ml-1"
>
<div className="flex flex-row items-center">
<FaEdit className="mr-2" /> Edit table
</div>
</Button>
<EditTableModal
show={modalShow}
onHide={() => setModalShow(false)}
backdrop={"static"}
columns={columns}
/>
</div>
<BootstrapTable
keyField="idInternal"
data={data}
columns={columns}
cellEdit={cellEditFactory({
mode: "click",
blurToSave: true,
autoSelectText: true
})}
/>
</div>
) : (
<div className="font-mono text-sm">No data in table</div>
)}
</div>
);
};
export default ComponentList;

Resources