Argument appears to not be a ReactComponent. Keys: - reactjs

I need to print a component for that i'm using react-to-print package.
here everything is working fine. when i try to customize it based on my requirement
i'm getting this error. i tried to search in the google but didn't get any proper solution.
so here i posted.
this is the component i want to print it.
const ModalContent=React.forwardRef(({ ticketDetails, bookingHistory, movie }, ref)=>{
var op = movie.filter((e, i, arr) => {
return e._id == bookingHistory.movieId
})
const [invoiceSummary, setinvoiceSummary] = useState({
totalSeatCount: bookingHistory.seatSeletion.length,
ticketPrice: op[0].ticketPrice,
gstRate: op[0].gst,
serviceCharge: op[0].serviceCharge
})
const [summaryFinalOk, setsummaryFinalOk] = useState(true);
useImperativeHandle(ref, () => ({
showTick() {
setsummaryFinalOk(true);
}
}));
return (<>
<div className="card">
{summaryFinalOk ?<>
<svg class="checkmark" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 52 52">
<circle class="checkmark__circle" cx="26" cy="26" r="25" fill="none" />
<path class="checkmark__check" fill="none" d="M14.1 27.2l7.1 7.2 16.7-16.8" />
</svg>
<h3>your Ticket has been Confirmed</h3></>
: <>
<div className="card-header bg-black"></div>
<div className="card-body">
<div className="row">
<div className="row text-center">
<h3 className="text-uppercase text-center mt-3 invoice-h3" >Invoice</h3>
</div>
<div className="row mx-3">
<table className="table">
<thead>
<tr>
<th scope="col">Ticket Order Summary </th>
<th scope="col">Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>Ticket price</td>
<td>{invoiceSummary.totalSeatCount} x {invoiceSummary.ticketPrice} = {invoiceSummary.totalSeatCount * invoiceSummary.ticketPrice}<i class="fa-solid fa-indian-rupee-sign"></i></td>
</tr>
<tr>
<td>Gst</td>
<td>{invoiceSummary.gstRate} % ={(invoiceSummary.totalSeatCount * invoiceSummary.ticketPrice) * invoiceSummary.gstRate / 100}<i class="fa-solid fa-indian-rupee-sign"></i></td>
</tr>
<tr>
<td>Internet Handling</td>
<td>{invoiceSummary.totalSeatCount} x{invoiceSummary.serviceCharge}={invoiceSummary.totalSeatCount * invoiceSummary.serviceCharge}<i class="fa-solid fa-indian-rupee-sign"></i></td>
</tr>
</tbody>
</table>
</div>
<hr></hr>
</div>
</div>
</>}
</div>
</>);
})
in index.js
import React, { memo, useEffect, useState, useRef, useCallback } from 'react';
import {useReactToPrint} from 'react-to-print';
import ModalContent from './modalContent';
function TicketBooking({ }) {
const tick=useRef();
//print the data
const handlePrint = useReactToPrint({
content: () => tick.current
});
const showcontent = ()=>{
}
return (
<div className="container screen-design">
<button onclick={showcontent}>showcontent</button>
<ModalContent ticketDetails={""} ref={tick} bookingHistory={[]} movie={""}></ModalContent>
<button onclick={handlePrint}>print</button>
</div>)
}
export default memo(TicketBooking);
so when i click the showcontent the button i can able to see the content. but when i click the print button i got following error.
can any one know how to solve this.

Related

How to pass partial data to a parent component in react

I have the following Component TBorrowed
import React, { Fragment, useState} from "react";
import {Link} from 'react-router-dom';
const EditItem = ({ item }) => {
const [name, setName] = useState(item.name)
const saveData = async (e) => {
e.preventDefault();
const body = { name}
await fetch(`http://127.0.0.1:5000/item/edit/${item.id}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body)
})
}
return (
<Fragment>
<Link className="link" data-toggle="modal" data-target={`#id${item.id}`} >{item.name}</Link>
<div className="modal" id={`id${item.id}`}>
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<h4 className="modal-title">Edit Item</h4>
</div>
<div className="modal-body">
<label>Name</label>
<input value={name} onChange={e => { setName(e.target.value) }} type="text" />
</div>
<div className="modal-footer">
<button onClick={e => { saveData(e) }} type="button" className="btn btn-outline-success ml-auto" data-dismiss="modal">Save</button>
</div>
</div>
</div>
</div>
</Fragment>
)
}
export default EditItem;
The above is called in another component, Main as shown below
import React, { useState} from 'react';
import TBorrowed from './TBorrowed';
const Main = () => {
const [items, setItems] = useState([]);
...MANY ITEMS SKIPPED...
return (
<table className="layout">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Code</th>
</tr>
</thead>
<tbody>
{
items.map((item, index) => (
<tr key={item.id}>
<td>{index + 1}</td>
<td>{item.name}</td>
<td>{<TBorrowed item={item} />}</td>
</tr>
))
}
</tbody>
</table>
)
}
export default Main;
The above works well where I am able to see the item code in the Main component's <td></td> when rendered, which when I click, I am able to edit the particular item in a modal.
My issue is I no longer want to edit an item in a modal but I want it rendered on it's own page for editing.
When I try it without a data-toggle = "modal" in the TBorrowed component, I get all the contents of the TBorrowed component displaying in the Main component where the modal is called i.e <td>{<TBorrowed item={item} />}</td>. All the data in TBorrowed is shown in that <td></td> instead of just the item.code as it was showing while using the modal
My code has some parts missing so it can fit here.
Please assist, and if there's more information required I'll provide it.

Nextjs / Firestore didn't work completely when i use environment variables

My project was working properly. Before publishing application, I wanted to save the firebaseconfig keys in the .env.local file and publish it. But when I did it this way, interestingly, there was no problem while getting data from the firestore, but when I want to send data, the function starts but never ends. I am not getting any error codes. I don't face any problem when I use original strings instead of environment variables. I'd appreciate it if anyone has a problem and can help. Below i am attaching the files i use firebase.
UPDATE: It worked only when I manually typed the "projectId" part and get rest with environment variables. I think it's a bug or something like that, I sent a support message to Google, if it works, I will let you know.
/firebase-config.js
import { getFirestore } from "firebase/firestore";
const firebaseConfig = {
apiKey: process.env.FIREBASE_API_KEY,
authDomain: process.env.FIREBASE_AUTH_DOMAIN,
projectId: process.env.FIREBASE_PROJECT_ID,
storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.FIREBASE_APP_ID,
};
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
export { db };
/components/SubscribeForm.js
import { addDoc, collection } from "firebase/firestore";
import { db } from "../firebase-config";
import { useState } from "react";
function SubscribeForm() {
const [loading, setLoading] = useState(false);
const [submit, setSubmit] = useState(false);
const [error, setError] = useState(false);
return (
<div className="col-lg-4 col-md-6 footer-newsletter form">
<h4>Join Our Newsletter</h4>
<p>Stay in tune with our latest projects.</p>
<Formik
initialValues={{
email: "",
}}
onSubmit={async ({ email }) => {
try {
console.log("start");
setLoading(true);
const docRef = await addDoc(collection(db, "subscribe-forms"), {
email,
});
console.log("finish");
setLoading(false);
setSubmit(true);
setTimeout(() => {
setSubmit(false);
}, 3000);
} catch (e) {
console.log("error");
console.log(e);
setError(true);
}
}}
>
{({ handleSubmit, handleChange, values }) => (
<form onSubmit={handleSubmit}>
<input
type="email"
name="email"
required
onChange={handleChange}
value={values.email}
/>
<input type="submit" value="Subscribe" />
</form>
)}
</Formik>
{loading && <div className="loading my-2">Loading</div>}
{error && (
<div className="error-message my-2">
Your message could not be sent. Please try again.
</div>
)}
{submit && (
<div className="sent-message my-2">
Your message has been sent. Thank you!
</div>
)}
</div>
);
}
export default SubscribeForm;
/pages/admin.js
import { collection, getDocs } from "firebase/firestore";
import { useSession, signIn, signOut } from "next-auth/react";
import { db } from "../firebase-config";
import Head from "next/head";
function Admin({ contactForms, subscribeForms }) {
const { data: session } = useSession();
return (
<>
<Head>
<title>Admin | Winniesoft</title>
</Head>
<section id="breadcrumbs" className="breadcrumbs">
<div className="container"></div>
</section>
<section className="inner-page">
<div className="container">
{!session && (
<div className="d-flex justify-content-center align-items-center">
<button
type="button"
className="btn btn-primary"
onClick={() => signIn()}
>
Sign in
</button>
</div>
)}
{session && (
<>
<div className="d-flex justify-content-end align-items-center">
<button
type="button"
className=" btn btn-secondary"
onClick={() => signOut()}
>
Sign out
</button>
</div>
<h2>Contact Forms</h2>
<div class="table-responsive">
<table className="table table-hover">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Email</th>
<th scope="col">Subject</th>
<th scope="col">Message</th>
</tr>
</thead>
<tbody>
{contactForms.map((form, i) => (
<tr key={i}>
<th>{i + 1}</th>
<td>{form.name}</td>
<td>{form.email}</td>
<td>{form.subject}</td>
<td>{form.message}</td>
</tr>
))}
</tbody>
</table>
</div>
<h2>Subscribe Forms</h2>
<div class="table-responsive">
<table className="table table-hover">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Email</th>
</tr>
</thead>
<tbody>
{subscribeForms.map((form, i) => (
<tr key={i}>
<th>{i + 1}</th>
<td>{form.email}</td>
</tr>
))}
</tbody>
</table>
</div>
</>
)}
</div>
</section>
</>
);
}
export default Admin;
export async function getServerSideProps() {
const contactForms = [];
const subscribeForms = [];
const contact = await getDocs(collection(db, "contact-forms"));
contact.forEach((doc) => {
contactForms.push(doc.data());
});
const subscribe = await getDocs(collection(db, "subscribe-forms"));
subscribe.forEach((doc) => {
subscribeForms.push(doc.data());
});
return {
props: { contactForms, subscribeForms },
};
}
https://ibb.co/bd2Gfsg

How to create multiple layouts in Next js?

I want to create another layout on my nextjs app. I saw this method somewhere, on adding multiple/nested layout in next js. The problem is I can't make this work, there's always this error:
Error: MyApp(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.
Now this is my code:
My _app.js
import Layout from "./../components/Layout";
function MyApp({ Component, pageProps }) {
const getLayout = Component.pageLayout || ((page) => <Layout>{page}</Layout>);
return getLayout(<Component {...pageProps} />);
}
export default MyApp;
This is my Layout
import React, { Fragment, useState, useEffect, useRef } from "react";
import { useRouter } from 'next/router';
import NavBar from "./../NavBar";
import MidNavBar from "./../MidNavBar";
import Banner from "./../Banner";
const Layout = ({ children, navBarRefProps }) => {
const navBarRef = useRef();
const router = useRouter();
const navBarRefPropsHandle = (props) => {
navBarRef.current = props.current;
navBarRefProps(navBarRef.current);
}
return(
<Fragment>
<div className="wrapper">
<NavBar navBarRefProps={ navBarRefPropsHandle } />
<div className="inner_wrapper">
<Banner/>
{
router.pathname !== "/" ?
<MidNavBar midNavBarRefProps = {midNavBarRefPropsHandle} dummyRefProps={dummyRefPropsHandle}/>
:
""
}
{ children }
</div>
</div>
</Fragment>
)
}
This is my AnotherLayout
const AnotherLayout = ({ children }) => {
return(
<Fragment>
<main>
<div>
<aside className="menu">
<div className="menu_header">
<div className="menu_title">
<span>
Panel
</span>
<button onClick={logoutHandle}>
logout
</button>
</div>
<button className="burger" onClick={openMenuHandle}>
<div className="burger-one">
</div>
<div className="burger-two">
</div>
<div className="burger-three">
</div>
</button>
</div>
<div ref={menuBodyRef} className="menu_body">
<ul>
<li>
<Link href={ url + "/rooms" }>
<div>
</div>
News
</Link>
</li>
<li>
<Link href={ url + "/events" }>
<div>
</div>
Events
</Link>
</li>
<li>
<Link href={ url + "/categories" }>
<div>
<i className="fas fa-snowboarding"></i>
</div>
Activities
</Link>
</li>
</ul>
</div>
</aside>
<section className="container_panel">
{ children }
</section>
</div>
</main>
</Fragment>
)
}
export default AnotherLayout;
And this is my index page
import React, { Fragment, useState, useEffect, useRef } from "react";
import AnotherLayout from "./../../../../components/AnotherLayout";
const Panel = () => {
// a lot of codes here.....
return(
<Fragment>
<section>
<div>
<div>
<h1><span>Rooms</span> Panel</h1>
</div>
<div className="container_table">
<table>
<thead>
<tr>
<td>Rooms</td>
<td>
<div>
<input type="search" name="search" placeholder="search"/>
</div>
</td>
<td className="col_add">
<button>
add
rooms
</button>
</td>
</tr>
</thead>
<tbody>
{
rooms.map(room => {
return(
<tr key={room._id}>
<td colSpan="2">
<button>{room.title}</button>
</td>
<td className="ud">
<button className="edit">
edit
</button>
<button className="delete">
delete
</button>
</td>
</tr>
)
})
}
</tbody>
<tfoot>
<tr>
<td>
</td>
</tr>
</tfoot>
</table>
</div>
</div>
</section>
</Fragment>
)
}
Panel.getLayout = (page) => {
<AdminLayout>
{ page }
</AdminLayout>
}
export default Panel;
Thank you!.
Solved this by adding return()
Panel.getLayout = (page) => {
return(
<AdminLayout>
{ page }
</AdminLayout>
)
}

How do I get my react app to display object information in my bootstrap modal when they click on a list item?

I've been trying for a while now, with no luck on trying to get it to where a user clicks on a table item that it brings up a window with the information on what they clicked. I have a JSON file and would like it to read the information from the file instead of the actual file page.
Here is the main app page
import './App.scss';
import list from './list.json';
import Table from 'react-bootstrap/Table';
import MyVerticallyCenteredModal from './components/modal';
function App() {
const [modalShow, setModalShow] = useState(false);
const [menu, setMenu] = useState(
{
food: ""
}
);
const handleChange = (event) => {
setMenu({
food: event.target.value
});
}
const [looped] = useState(
list.map((obj, i) => {
return (
<tr key={i} onClick={() => {
setModalShow(true);
handleChange(obj.food)
console.log(obj.food)
}
}>
<td>{i + 1}</td>
<td>{obj.food}</td>
<td>{obj.description}</td>
<td>${obj.price.toString()}</td>
</tr>
)
})
);
// console.log(menu);
return (
<div className="menu-template container-lg">
<div className="mt-5 mb-5 d-flex">
<Table striped bordered hover>
<thead>
<tr>
<th>#</th>
<th>Meal Name</th>
<th>Description</th>
<th>Price</th>
</tr>
</thead>
<tbody>
{
looped
}
</tbody>
</Table>
</div>
<MyVerticallyCenteredModal
food={food}
description={description}
price={price}
show={modalShow}
onHide={() => setModalShow(false)}
/>
</div>
);
}
export default App;
Here is the modal
import Modal from 'react-bootstrap/Modal';
import Button from 'react-bootstrap/Button';
import image from '../assets/image1.jpg';
function MyVerticallyCenteredModal(props) {
return (
<Modal
{...props}
size="lg"
aria-labelledby="contained-modal-title-vcenter"
centered
>
<Modal.Header>
<Modal.Title id="contained-modal-title-vcenter">
<img src={image} alt="..." width="100%" />
</Modal.Title>
</Modal.Header>
<Modal.Body>
<h4>
Some Tasty Food!</h4>
<p>
This is the best food you've ever eaten!
</p>
</Modal.Body>
<Modal.Footer>
<Button onClick={props.onHide} variant="danger">Close</Button>
</Modal.Footer>
</Modal>
);
}
export default MyVerticallyCenteredModal;```
From the first look at this, it doesn't look like you are adding an event to the handleChange() function, just passing a parameter from the obj dictionary. Also, since you are only showing a modal when there is a value in the menu, I would just use that as the indicator to show.
Here's a small sample:
const [menu, setMenu] = useState(null);
const [looped] = useState(
list.map((obj, i) => {
return (
<tr key={i} onClick={() => setMenu({food: obj.food, description: obj.description, price: obj.price, <etc...>})}>
<td>{i + 1}</td>
<td>{obj.food}</td>
<td>{obj.description}</td>
<td>${obj.price.toString()}</td>
</tr>
)
})
);
// console.log(menu);
return (
<div className="menu-template container-lg">
<div className="mt-5 mb-5 d-flex">
<Table striped bordered hover>
<thead>
<tr>
<th>#</th>
<th>Meal Name</th>
<th>Description</th>
<th>Price</th>
</tr>
</thead>
<tbody>
{
looped
}
</tbody>
</Table>
</div>
<MyVerticallyCenteredModal
{...menu} /** Just spreading out the dictionary from the state */
show={!!menu} // Double ! makes it a true or false
onHide={() => setMenu(null)} // or false..
/>
</div>
);
} ```
----------
Because you have now passed the props from that state, you can now use
them on the modal like this:
function MyVerticallyCenteredModal(props) {
const {food, descriptions, price} = props
console.log('view passed props')
....
}
In the model file.
When you're exporting the component remove default.
Use something like this-
export {MyVetrical....}
Lemme know if it works

how to react-js-pagination implement with react hook data table

How to integrate pagination code with hooks method data table with. im using react-js-pagination nmp package but there is no one explanation for implement with hook method program.
This my data table code:
import React, { useEffect, useState } from 'react';
import axios from 'axios'
import 'bootstrap/dist/css/bootstrap.min.css';
import {Link} from 'react-router-dom';
const ProTable = () => {
const [data, setData] = useState([]);
useEffect(() => {
loadData();
}, []);
const loadData = async() => {
axios.get('http://localhost:5000/api/clientgetdata')
.then(response => {
setData(response.data.map);
}
const delPro = (item,e) => {
var option = window.confirm(`Are you sure to delete ${e.clientName} OF ${item.projectName}`)
if(option){
const check = axios.delete(`http://localhost:5000/api/clientdelpro/${e.clientName}/${item.projectName}`).then(res => {
//console.log(clientname)
window.location.reload(false)
})
}
}
return (
<>
<div className="row addButton">
<div className="col-lg-1">
<Link
className="btn btn-outline-primary mr-2"
to={'/client/addpro'}
>New</Link>
</div>
<div className="col-lg-1">
{/* <button variant="primary" >Delete</button> */}
</div>
</div>
<div className="row hrtable">
<div className="col-lg-10 col-sm-6 col-md-6">
<div className="table-responsive tcenter" >
<table className="table table-bordered table-hover table-sm">
<thead className="thead-dark">
<tr>
<th scope="col"><input type="checkbox" /></th>
<th scope="col">Client Name</th>
<th scope="col">Project Name</th>
<th scope="col">Status</th>
<th>Action</th>
</tr>
</thead>
{ (data.length > 0) ? data.map( e => {
return (
<>
{e.project.map(item=> {
return (
<tbody>
<tr>
<th scope="row">
<input type="checkbox"/>
</th>
<td><ul>{e.clientName}</ul></td>
<td><ul>{item.projectName}</ul></td>
<td><ul>{item.proStatus}</ul></td>
<td>
<Link
className="btn btn-outline-primary mr-2"
to={`/project/edit/${e.clientName}/${item.projectName}`} >
Edit
</Link>
<button
className="btn btn-danger"
onClick={() => delPro(item,e)}>
Delete
</button>
</td>
</tr>
</tbody>
);
})}
</>
);
}) : <tr><td colSpan="5">No Records Found</td></tr> }
</table>
</div>
</div>
</div>
</>
);
}
export default ProTable;
This is Reaci-js-pagination code.
I am trying to follow this tutorial to create a pagination in my application https://www.npmjs.com/package/react-js-pagination#usage
import React, { Component } from "react";
import ReactDOM from "react-dom";
import Pagination from "react-js-pagination";
require("bootstrap/less/bootstrap.less");
class App extends Component {
constructor(props) {
super(props);
this.state = {
activePage: 15
};
}
handlePageChange(pageNumber) {
console.log(`active page is ${pageNumber}`);
this.setState({activePage: pageNumber});
}
render() {
return (
<div>
<Pagination
activePage={this.state.activePage}
itemsCountPerPage={10}
totalItemsCount={450}
pageRangeDisplayed={5}
onChange={this.handlePageChange.bind(this)}
/>
</div>
);
}
}
plz help me how integrate both code
Try by converting it into hooks
const [state, setState] = React.useState({activePage: 15});
const handlePageChange=(pageNumber) => {
setState({activePage: pageNumber});
// make api call for next page
}
return (
<div>
<Pagination
activePage={state.activePage}
itemsCountPerPage={10} // pass your fixed item per pages
totalItemsCount={450} // total item -> per-page * no of page
pageRangeDisplayed={5}
onChange={handlePageChange}
/>
</div>
);

Resources