Im looking for a solution to build a Template website in React which displays a Carousel with 3 different Images on the Left Part and a Informational Part on the Right with A Title, some Attributes and a Description. I dont want to Copy and Paste the Website all the Time to rewrite every element and Change the Pictures. The Barebone of my Page is done, i just need the Solution to probably make an Array and depended on the Image which is clicked, youre getting to the mentioned DescriptionPage filled with the Information based on the clicked Image.
const DescriptionPage = () => {
return (
<>
<div className='CardDescriptionContainer'>
<Carousel>
<CarouselItem><img alt="Fool" className='image-carousel' src={Fool} draggable="false"/></CarouselItem>
<CarouselItem><img alt="Fool" className='image-carousel' src={ThothFool} draggable="false"/></CarouselItem>
<CarouselItem><img alt="Fool" className='image-carousel' src={JDFool} draggable="false"/></CarouselItem>
</Carousel>
<div className='CardDescription '>
<div className='Title'>
<h1>The Fool</h1>
</div>
<div className='Element'>
<h2>Element</h2>
<p>Air</p>
</div>
<div className='Zodiac'>
<h3>Zodiac / Planet</h3>
<p>Uranus</p>
</div>
<div className='Qualities'>
<h3>Qualities</h3>
<p>Freedom <br/>
Lust for Life<br/>
Beginnings<br/>
Adventure</p>
</div>
<div className='Symbols'>
<h3>Symbols</h3>
<p> White Rose <br/>
Small Bundle<br/>
Small Animal<br/>
Precipice</p>
</div>
<div className='ShortDescription'>
<h2>Description</h2>
<p>Into the Unkown.... this is placeholder Text for the Fool as he is, a Placeholder, the nothing before there is with all the Options to go anywhere and be anyone he'd like to. Pure AIR!</p>
</div>
</div>
</div>
</>
)
}
export default DescriptionPage
I was trying to find the Correct Syntax and was thinking about Mapping over an Array containing the Data but i dont know how to choose the correct Array or Dataset based on the clicked Image. Would i use useState to change the State of the Page itself or is it the best approach to "load" the Description Page with the Data stored in a different file?
You can write like this, if I understand you correctly
const INTIAL_VALUES = [
{
className: "Title",
heading: "The Fool",
paragraph: ""
},
{
className: "Element",
heading: "Element",
paragraph: "Air"
}, {
className: "Zodiac",
heading: "Zodiac / Planet",
paragraph: "Uranus"
}, {
className: "Qualities",
heading: "Qualities",
paragraph: "Freedom Lust for Life Beginnings Adventure"
}]
const DescriptionPage = () => {
const [state, setState] = useState(INTIAL_VALUES)
return (
<>
<div className='CardDescriptionContainer'>
<Carousel>
<CarouselItem><img alt="Fool" className='image-carousel' src={Fool} draggable="false" /></CarouselItem>
<CarouselItem><img alt="Fool" className='image-carousel' src={ThothFool} draggable="false" /></CarouselItem>
<CarouselItem><img alt="Fool" className='image-carousel' src={JDFool} draggable="false" /></CarouselItem>
</Carousel>
<div className='CardDescription '>
{state.map((element, key) => (<div key={key} className={element.className}>
<h3>{element.heading}</h3>
<p>{element.paragraph}</p>
</div>))}
</div>
</div>
</>
)
}
export default DescriptionPage
Related
I need some help with flexbox. I'm not able to evenly distribute all components in a row with 'justify-content' - the components only stay in a column. It seems that React does not consider the 2 components that were generated from props - it treats all as just one component.
App.js
const App=()=> {
return(
<>
<Header />
<div className="containerHeader">
<h1>RECEIVE THE BEST DENTAL TREATMENT TO AVOID CROOKED-TEETH PROBLEM</h1>
<h2>We will give you the accurate assessment to find out which braces best suit you.</h2>
</div>
{Data.map((values)=>{
return(
<>
<Meeting
key={values.id}
imgsrc={values.imgsrc}
text={values.text}
/>
</>
)
})}
</>
);
};
Meeting.js
const Meeting=(props)=> {
return(
<div className="containerMeeting">
<div className="containerCard">
<img src={props.imgsrc} alt="Girl" />
<p>{props.text}</p>
</div>
</div>
);
};
Meeting.css
.containerMeeting {
padding: 0px 20px;
display: flex;
flex-direction: row;
justify-content: space-between;
}
MeetingData.js
const Data=[
{
id:1,
imgsrc:"../Images/1.jpg ",
text:"Well-aligned teeth and fresher breath always leave a good impression when meeting a person for the first time."
},
{
id:2,
imgsrc:"../Images/2.jpg ",
text:"Even in a job interview your smile can lead to a favourable situation"
}
]
export default Data;
I hope that React considers the 2 components and distribute them horizontally.
"It seems that React does not consider the 2 components that were generated from props", there is only one containerCardelement inside the containerMeeting element. Thus nothing to align in a row.
When you map over Data what it will return will look like this
<div className="containerMeeting">
<div className="containerCard">
<img src="../Images/1.jpg " alt="Girl" />
<p>Well-aligned teeth and fresher breath always leave a good impression when meeting a person for the first time.</p>
</div>
</div>
<div className="containerMeeting">
<div className="containerCard">
<img src="../Images/2.jpg " alt="Girl" />
<p>Even in a job interview your smile can lead to a favourable situation</p>
</div>
</div>
If you want to make your containerCard elements appear in a row you should move the containerMeeting element out of the Meeting component and use it to surround Data.map
const Meeting = (props) => {
return (
<div className="containerCard">
<img src={props.imgsrc} alt="Girl" />
<p>{props.text}</p>
</div>
);
};
<div className="containerMeeting">
{Data.map((values) => {
return (
<Meeting key={values.id} imgsrc={values.imgsrc} text={values.text} />
);
})}
</div>
i am currently working on chat application with sockets , when i get different messages i use an array and then use map method to display them in simple html tags like p etc it worked perfect but inside text-area its not working i also tried to set text-area value property with map method but only i see is [object object] . also how can i automatically move the scroll bar down when messages there are more messages.
here is the code
import { Fragment, useEffect } from "react";
import { format } from "date-fns";
const Chat = ({ name, message }) => {
const date = new Date();
const hour = date.getHours();
const minute = date.getMinutes();
const second = date.getSeconds();
console.log("so bteay", message);
return (
<Fragment>
<div>
<h3 className="d-inline-block me-3"> Chat log </h3>
{name && (
<span className="me-3 d-inline-block">
<div
class="spinner-grow spinner-grow-sm text-success"
style={{ fontSize: "10px" }}
role="status"
>
<span class="visually-hidden">Loading...</span>
</div>
</span>
)}
<small className="text-muted d-block "> {name}</small>
<textarea
cols="70"
rows="8"
value={message.map((eachMsg) => {
return (
<Fragment>
{
<small className="text-muted d-inline-block">{`${hour}:${minute}:${second}`}</small>
}
<p
className="d-block shadow p-1 fw-bold rounded text-success"
style={{ fontFamily: "cursive" }}
>
{eachMsg}
</p>
</Fragment>
);
})}
></textarea>
</div>
</Fragment>
);
};
export default Chat;
You can only pass 1 child ( text in this case ) to text area. But you are trying to pass an array. If what you meant to do is to have as many as textareas as your array, this is how you should go about it:
const texts = ["Hi", "Bye","done"];
<div>
{texts.map((text) => (
<textarea>text</textarea>
))}
</div>
but if you are trying to have 1 textarea with all your texts inside it, first you need to create a long string using join method, and then render that string.
I think that you can't set html code inside textarea, unless you want to show it as a text?
** I want to change the image src on mouseover, i have added multiple images dynamically.**
const Servicesdata = [
{
ID: "01",
title: "Power Generation",
desc:
" We have rich experience in building thermal, hydro, and combined cycle power plants. We provide customized ready-to-deploy solutions for power plants including total EPC and comprehensive Balance of Plant (BOP) and Flue-gas desulfurization (FGD) solutions.",
imgsrc: "https://www.tataprojects.com/images/Transmission-Line.jpg",
imgsrcHover: "https://www.tataprojects.com/images/Sunbstations-min.jpg"
},
{
ID: "02",
title: "Transmission",
desc:
"We have successfully commissioned more than 13,000 kms of transmission lines across multiple voltage levels including 800kv HVDC projects",
imgsrc: "https://www.tataprojects.com/images/Sunbstations-min.jpg",
imgsrcHover: "https://www.tataprojects.com/images/Sunbstations-min.jpg"
},
{
ID: "03",
title: "Substations",
desc:
"Our optimally designed towers and substation structures allow us to reduce conductor wastage ensuring faster construction and on-time delivery.",
imgsrc: "https://www.tataprojects.com/images/Tower-Manufactaring-Unit.jpg",
imgsrcHover: "https://www.tataprojects.com/images/Sunbstations-min.jpg"
},
{
ID: "04",
title: "Tower Manufacturing Unit",
desc:
"We have a state-of-the-art manufacturing unit to manufacture transmission line towers and structures. The unit is spread across 40 acres of land.",
imgsrc: "https://www.tataprojects.com/images/Smart-Grid-min.jpg",
imgsrcHover: "https://www.tataprojects.com/images/Sunbstations-min.jpg"
}
];
export default Servicesdata;
import react from "react";
import Servicesdata from "../data/Servicesdata";
const Services = () => {
return (
<>
<section className="services">
<div className="container mt-5">
<div className="row">
<div className="col-md-12">
<h2 className="text-center heading-style-1">Key Areas</h2>
</div>
</div>
{Servicesdata.map((val, index) => {
return (
<div className="row featurette align-items-center">
<div className="col-md-7">
<h2 className="featurette-heading">{val.title}</h2>
<p className="lead">{val.desc}</p>
</div>
<div className="col-md-5">
<img src={val.imgsrc} className="img-fluid" />
</div>
</div>
);
})}
</div>
</section>
</>
);
};
export default Services;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
We can make use of onMouseOver & onMouseOut event handlers in order to toggle the images of the current hovering image.
We can store the ID of the object in the state when we hover on the image of that particular object
And reset it to "" on mouse out
In render we can check the ID in the state with the object id, if they are matching then use imgsrcHover else use imgsrc
const Servicesdata = [{ID:"01",title:"Power Generation",desc:" We have rich experience in building thermal, hydro, and combined cycle power plants. We provide customized ready-to-deploy solutions for power plants including total EPC and comprehensive Balance of Plant (BOP) and Flue-gas desulfurization (FGD) solutions.",imgsrc:"https://www.tataprojects.com/images/Transmission-Line.jpg",imgsrcHover:"https://www.tataprojects.com/images/Sunbstations-min.jpg"},{ID:"02",title:"Transmission",desc:"We have successfully commissioned more than 13,000 kms of transmission lines across multiple voltage levels including 800kv HVDC projects",imgsrc:"https://www.tataprojects.com/images/Sunbstations-min.jpg",imgsrcHover:"https://www.tataprojects.com/images/Sunbstations-min.jpg"},{ID:"03",title:"Substations",desc:"Our optimally designed towers and substation structures allow us to reduce conductor wastage ensuring faster construction and on-time delivery.",imgsrc:"https://www.tataprojects.com/images/Tower-Manufactaring-Unit.jpg",imgsrcHover:"https://www.tataprojects.com/images/Sunbstations-min.jpg"},{ID:"04",title:"Tower Manufacturing Unit",desc:"We have a state-of-the-art manufacturing unit to manufacture transmission line towers and structures. The unit is spread across 40 acres of land.",imgsrc:"https://www.tataprojects.com/images/Smart-Grid-min.jpg",imgsrcHover:"https://www.tataprojects.com/images/Sunbstations-min.jpg"}];
const { useState } = React;
const Services = () => {
//Store the currently hovered object's id in the state
//Initially it'll be ""
const [currentHoveredId, setCurrentHoveredId] = useState("");
//On mouse over update the id with the cuurent object's ID
const onMouseOver = (id) => {
setCurrentHoveredId(id);
}
//On moving the cursoe out of the image, then reset it to ""
const onMouseOut = () => {
setCurrentHoveredId("");
}
return (
<section className="services">
<div className="container mt-5">
<div className="row">
<div className="col-md-12">
<h2 className="text-center heading-style-1">Key Areas</h2>
</div>
</div>
{Servicesdata.map((val, index) => {
return (
<div className="row featurette align-items-center" key={val.ID}>
<div className="col-md-7">
<h2 className="featurette-heading">{val.title}</h2>
<p className="lead">{val.desc}</p>
</div>
<div className="col-md-5">
{/* Toggle the image source based on the result of the id in state and the id of the current object */}
<img src={currentHoveredId === val.ID ? val.imgsrcHover : val.imgsrc}
className="img-fluid"
onMouseOver={() => {onMouseOver(val.ID)}}
onMouseOut={onMouseOut}/>
</div>
</div>
);
})}
</div>
</section>
);
};
ReactDOM.render(<Services />, document.getElementById("react"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.0/umd/react-dom.production.min.js"></script>
<div id="react"></div>
I have used your code as it is just added the corresponding event handlers and the state as mentioned above.
Make the following modifications in the code:
this.setState({
servicesData: ServicesData;
})
And call the below function on mouseover,passing the index and the newSrc as parameter:
imgSrcUpdate(index, newSrc) {
let oldData = this.state.servicesData;
oldData[index][src] = newSrc;
this.setState({
servicesData: oldData
})
}
Instead of this:
{Servicesdata.map((val, index) => { ...
Use :
{this.state.servicesData.map((val, index) => {....
The state
state = {
posts: [],
}
The posts gets an API value of
{
"authors": [
{
"authorName": "Sideney Sheldon",
"authorImageURL": "http..."
},
{
"authorName": "Sideney Sheldon",
"authorImageURL": "http..."
},
....
]
}
code
render(){
const postList = posts.length ? (
posts.map(post => {
return (
<div className="" key={this.state.key}>
<div className="containerimager">
<img style={imgb} src={post.authorImageURL} />
</div>
<h6 style={{ marginTop:"23px"}} >{post.authorName}</h6>
</div>
)
})
) :
(
<div className="center">No posts to show</div>
);
return(
{postList}
)
}
The {postlist} returns objects but it returns each object in a new row in the webpage
I want all the results(images) in the same row
can anyone please help?
If you want multiple elements in the same row, you could achieve that with css. Use display: flex(by default flex-direction is row which is what you need) on the wrapper to postList. You can either pass it as a style though JSX or add a class that specifies this property
render(){
const postList = posts.length ? (
posts.map(post => {
return (
<div className="" key={this.state.key}>
<div className="containerimager">
<img style={imgb} src={post.authorImageURL} />
</div>
<h6 style={{ marginTop:"23px"}} >{post.authorName}</h6>
</div>
)
})
) : (
<div className="center">No posts to show</div>
);
return(
<div style={{display: 'flex'}}>{postList}</div>
)
}
Here's a CodeSandbox sample I created using your code.
If you're trying to line up the users horizontally (from left to right), CSS is the way to go. In particular, the <div> that is being mapped has, by default, the display: block style which makes it occupy its own row in the page.
To line up divs side by side, you need to somehow apply the display: inline-block style. In my example I used a style prop, but you will likely want to do it in CSS directly.
This is a CSS problem, you should use display: flex together with flex-direction: row on the parent div which will provide the desired result.
posts.map(post,index => {
return (
<div className="" key={this.state.key}>
<div className="containerimager">
<img style={imgb} src={post.authors[index].authorImageURL} />
</div>
<h6 style={{ marginTop:"23px"}} >{post.authors[index].authorName}</h6>
</div>
)
})
) : (
<div className="center">No posts to show</div>
);
Try this
I am trying to render image from array by placing its name in middle of <img> link.
At this time I have ran out of links that could help me...
I tried everything that came to my mind. Everything displays as it should except image...!
this is array:
cars: [
{
'carname': '2013 Toyota GT86',
'owner': 'Ryan',
'desc': 'Aero Paket, BC Coilover, 3sdm 0.01 black matte, Invidia N1 Catback, Valenti taillights, camouflage vinyls, Vortech supercharger incl. oil cooler - 325hp daily driver',
'image': 'toyota'
}]
Render section:
render() {
const cars = this.state.cars.map((car, i) => {
return (
<div key={i}>
<div className="col-md-12 cBusiness">
<h2>
{ car.carname }
</h2>
<p>Owner: { car.owner }</p>
<p>{ car.desc }</p>
<p>
<img src={"pages/upload/{car.image}.jpg"} />
</p>
</div>
</div>
);
});
structure:
Do something like this:
<img src={`pages/upload/${car.image}.jpg`} />
Try using template literals:
<img src={`pages/upload/${car.image}.jpg`} />
or old skool for all the IE folks out there:
<img src={"pages/upload/" + car.image + ".jpg"} />
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals