React duplicate component getting displayed using layout - reactjs

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!

Related

Why do my pictures not render when using react?

I used default create-react-app to create a react app. However, when i try to add cards that contain pictures my image does not render. I get a white screen....
My image is located in a folder in src called Images. The rest works until I add in the Card prop and then the screen goes fully white. So I suppose that means it is not working or rendering properly.
import React from 'react';
export default function Card(props) {
return (
<div className="card">
<img src={require("./Images/monstera.jpeg")} alt="monstera"></img>
<div className="card-stats">
<p>{props.name}</p>
<p>{props.price}</p>
</div>
</div>
)
}
import logo from './logo.svg';
import './App.css';
import React from 'react';
import Header from './Components/Header';
import Inventory from './Components/Inventory';
import Cart from './Components/Cart';
import data from './data';
import Card from './Components/Card';
function App() {
return (
<div className="App">
<Header></Header>
<div className="row">
<Inventory>
<div className="cards-list">
<Card>
</Card>
</div>
</Inventory>
<Cart></Cart>
</div>
</div>
);
}
export default App;
Is there something I am doing wrong? Is there a special way to render a react app with an image?
look for any error through console and try cleaning your code a little
turn this
function App() {
return (
<div className="App">
<Header></Header>
<div className="row">
<Inventory>
<div className="cards-list">
<Card>
</Card>
</div>
</Inventory>
<Cart></Cart>
</div>
</div>
);
}
export default App;
into this
function App() {
return (
<div className="App">
<Header />
<div className="row">
<Inventory />
<div className="cards-list">
<Card />
</div>
<Cart />
</div>
</div>
);
}
export default App;
if not required don't use so many div

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

Image doesn't display React

I'm building a website by react and my local image doesn't display. I use props to pass the properties from Cards.js to CardItem.js then every properties display except image. I don't know what is a problem with my code :(
Here is Cards.js:
import React from 'react'
import CardItem from './CardItem'
import './Cards.css'
function Cards() {
return (
<div className="cards">
<h1>Check out these EPIC Destinations!</h1>
<div className="cards-container">
<div className="cards-wrapper">
<ul className="cards-items">
<CardItem
src='../assets/images/img-9.jpg'
text='Explore the hidden waterfall deep inside the Amazon Jungle'
label='Adventure'
path='/sevices'
/>
</ul>
</div>
</div>
</div>
)
}
export default Cards
CardItem.js:
import React from 'react'
import { Link } from 'react-router-dom'
function CardItem(props) {
return (
<>
<li className="cards-item">
<Link className="cards-item-link" to={props.path}>
<figure className="cards-item-pic-wrap" data-category={props.label}>
<img src={props.src} alt="Travel Img" className="cards-item-img" />
</figure>
<div className="cards-item-info">
<h5 className="cards-item-text">{props.text}</h5>
</div>
</Link>
</li>
</>
)
}
export default CardItem
we want to import the image first
import img from './assets/images/img-9.jpg';
We named image as img use it in your code.
import React from 'react'
import CardItem from './CardItem'
import './Cards.css'
import img from './assets/images/img-9.jpg';
function Cards() {
return (
<div className="cards">
<h1>Check out these EPIC Destinations!</h1>
<div className="cards-container">
<div className="cards-wrapper">
<ul className="cards-items">
<CardItem
src={img}
text='Explore the hidden waterfall deep inside the Amazon Jungle'
label='Adventure'
path='/sevices'
/>
</ul>
</div>
</div>
</div>
)
}
export default Cards
first import image
import img from '../assets/images/img-9.jpg'
then use it
<CardItem src={img} .../>
I hope this helps you, resources https://create-react-app.dev/docs/using-the-public-folder/:
for Cards.js file:
...
<CardItem
src='/images/img-9.jpg'
text='Explore the hidden waterfall deep inside the Amazon Jungle'
label='Adventure'
path='/services'
/>
And...
for CardItem.js file:
...
<img
className='cards__item__img'
alt='Travel'
src={process.env.PUBLIC_URL + props.src}
/>

Collapse React-Bootstrap Accordion from Within Accordion.Collapse component

I'm trying to pass a decorated onClick function returned from the react-bootstrap useAccordionToggle function into a component so that I can hide the Accordion from an interaction on that component. Something goes wrong and the decorated function that is created is no longer able to correctly access the contexts that react-bootstrap sets up (it seems to choke on SelectableContext here). I'm fairly new to react, and just learned about contexts while looking into why this is not working, I wouldn't be surprised if that is where my confusion comes from. It seems to not be getting the correct context at the time of the method construction.
I have included a small test case Click Here for a fiddle:
import React from "react";
import ReactDOM from "react-dom";
import { Button, Accordion, Card, useAccordionToggle } from "react-bootstrap";
import "bootstrap/dist/css/bootstrap.min.css";
import "./styles.css";
const InternalComponent = props => {
console.log(props.onClick);
return (
<div>
<button onClick={() => props.onClick()}>here for seeing</button>
</div>
);
};
function App() {
const x = useAccordionToggle(0, () => {
/**/
});
return (
<div className="App">
<>
<Accordion>
<Card>
<Card.Header>
<Accordion.Toggle as={Button} variant="link" eventKey="0">
create new group
</Accordion.Toggle>
</Card.Header>
<Accordion.Collapse eventKey="0">
<Card.Body>
<InternalComponent onClick={x} />
</Card.Body>
</Accordion.Collapse>
</Card>
</Accordion>
</>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
I don't have a great answer to this, but what I did for this myself was to have the inside component do onclick={document.querySelector("yourtogglecomponent").click()} to trigger my valid accordion.toggle outside the accordion.collapse. In theory you could have a hidden accordion.toggle somewhere and do the same.
You need to use useRef hook in order to perform a click in the accordion toggle.
Just create a constant for the ref, apply it to the Accordion.Toggle, and call it from the onClick at the desired component (in this case,InternalComponent).
import { useRef } from 'react';
function App() {
const accordionRef = useRef(null);
function toggleAccordion () {
accordionRef.current.click();
}
return (
<div className="App">
...
<Accordion.Toggle ref={accordionRef} as={Button} variant="link" eventKey="0">
...
<Card.Body>
<InternalComponent onClick={toggleAccordion} />
</Card.Body>
...

How to enable the React Semantic UI Modal to stretch across the full screen?

This is my first time trying to use Meteor with ReactJs and React Semantic UI, and having issues on rendering the Semantic UI modal. What I am trying to achieve is to click on the button and open up the modal overlaying on the whole browser, in reference to the React Semantic Modal Manual But what I have right now is that the modal is rendered partially on the screen, as seen from the attached screenshots. Can anyone please help? Thanks in advance.
Main.js
import {Meteor} from 'meteor/meteor';
import React from 'react';
import ReactDOM from 'react-dom';
import {routes} from "../imports/routes/routes";
Meteor.startup(() => {
ReactDOM.render(routes, document.getElementById('app'));
});
Site.js:
import React from 'react';
import { Header, Button, Modal } from 'semantic-ui-react';
import PrivateHeader from './PrivateHeader';
import Sidebar from './Sidebar';
import ModalExample from './Modal';
export class Site extends React.Component {
render() {
return (
<div>
<PrivateHeader/>
<Sidebar/>
<div className="page">
<div className="ui padded one column grid">
<div className="column">
<ModalExample/>
</div>
</div>
</div>
</div>
);
}
}
export default Site;
ModalExample.js
import React from 'react'
import { Button, Header, Icon, Modal } from 'semantic-ui-react'
const ModalBasicExample = () => (
<Modal trigger={<Button>Basic Modal</Button>} basic size='fullscreen'>
<Header icon='archive' content='Archive Old Messages' />
<Modal.Content>
<p>Your inbox is getting full, would you like us to enable automatic archiving of old messages?</p>
</Modal.Content>
<Modal.Actions>
<Button basic color='red' inverted>
<Icon name='remove' /> No
</Button>
<Button color='green' inverted>
<Icon name='checkmark' /> Yes
</Button>
</Modal.Actions>
</Modal>
)
export default ModalBasicExample
Sidebar.js
export const Sidebar = (props) => {
return (
<div className="ui inverted vertical left fixed menu" >
<a className="item" href="/">
<img src='/images/logo.png' className="ui mini right spaced image"/>
Semantics UI Test
</a>
<div className="item">
<div className="header">Hosting</div>
<div className="menu">
<a className="item">Shared</a>
<a className="item">Dedicated</a>
</div>
</div>
<div className="item">
<div className="header">Support</div>
<div className="menu">
<a className="item">E-mail Support</a>
<a className="item">FAQs</a>
</div>
</div>
</div>
)
};
export default Sidebar;
Thanks to Saad's comment mentioning the className that I found out that it was due to Semantic UI will add a ui page modals dimmer transition visible active className when the modal is opened. This causes a conflict to my existing page className which is used in other pages, but because of Meteor and React, the various scss are compressed together.
I suggest to run this.show('fullscreen') with an onClick event on trigger button like the following :
<Button onClick={this.show('fullscreen')}>Show Modal</Button>
ModalExample.js
import React, { Component } from 'react'
import { Button, Modal } from 'semantic-ui-react'
class ModalExample extends Component {
state = { open: false }
show = size => () => this.setState({ size, open: true })
close = () => this.setState({ open: false })
render() {
const { open, size } = this.state
return (
<div>
<Button onClick={this.show('fullscreen')}>Show Modal</Button> // Triggering button
<Modal size={size} open={open} onClose={this.close}>
<Modal.Header>
Delete Your Account
</Modal.Header>
<Modal.Content>
<p>Are you sure you want to delete your account</p>
</Modal.Content>
<Modal.Actions>
<Button negative>
No
</Button>
<Button positive icon='checkmark' labelPosition='right' content='Yes' />
</Modal.Actions>
</Modal>
</div>
)
}
}
export default ModalExample
Source

Resources