inline style css background in react not showing - css

What is the problem in my code? This is a slider:
<Slider {...settings}>
{slides.map(function(item){
return (
<div key={item.id} className="item-slider"
style={{background: `url(images/covers/${item.cover})`}}>
<div className="caption">
<h4>{item.topic}</h4>
<p>{item.title}</p>
</div>
</div>
)
})}
</Slider>
I'm using react-slick, and I tested if the item.cover is receiving some data, and it did receive data. but when I put the data in the style it does not appear inspecting it and it does not receive any errors.
Sample: code here

I had the same issue, try wrapping every slide into a div like this:
<Slider {...settings}>
<div>
<div className='slider__image' style={{'backgroundImage': `url(${House})`}} />
</div>
<div>
<div className='slider__image' style={{'backgroundImage': `url(${Map})`}} />
</div>
<div>
<div className='slider__image' style={{'backgroundImage': `url(${NotAvailable})`}} />
</div>
</Slider>
This way I got it working.

I think I got it. Try importing the image at the top of the file, and then using the import variable as the url. I got it to work:
https://codesandbox.io/s/xvnq2q21w4
import photo from "../img/reed.jpg";
const styles = {
background: `url(${photo})`
};

Below is an example I tried to load images in the background and it worked :)
Sample response. Here you can see, I have added require.
const items = [{
name: 'hola',
background: require('./img-placeholder.png')
},{
name: 'cola',
background: require('./img-placeholder.png')
},{
name: 'lola',
background: require('./img-placeholder.png')
}]
const renderImages = items.map((val, key) => {
return <div key={key} >
<div style={{ background: `url(${placeholder})`, height: 200 }}>
<div className="caption">
<h4>{val.name} - {key}</h4>
</div>
</div>
</div>
});
Note : The require() method is used to load and cache JavaScript modules. So, if you want to load a local, relative JavaScript module into a Node.js application, you can simply use the require() method.

You missing to define:
require()
height image
backgroundRepeat
<Slider {...settings}>
{slides.map(function(item){
console.log(item.cover, 'check this');
return (
<div key={item.id} className="item-slider" style={{
background: 'url(' + require(`./images/covers/${item.cover}`) + ')', backgroundRepeat: 'no-repeat', height: 300 }}>
<div className="caption">
<h4>{item.topic}</h4>
<p>{item.title}</p>
</div>
</div>
)
})}
</Slider>
Update: as you mentioned in comments that the path is from the public folder use process.env.PUBLIC_URL to access public folder
<div key={item.id} className="item-slider" style={{
background: 'url(' + process.env.PUBLIC_URL +`/images/covers/${item.cover}` + ')', backgroundRepeat: 'no-repeat', height: 300 }}>

You have to import image at the top of your file as
import myimage from './images/covers/imagename';
Also you can use dynamic importing like :
import(`${path}`)
.then(module => this.setState({ module: module.default }))
Then you can use image, where cover would be import name like:
<Slider {...settings}>
{slides.map(function(item){
import(`${path}`)
.then(module =>
return (
<div key={item.id} className="item-slider"
style={{background: `url(images/covers/${item.cover})`}}>
<div className="caption">
<h4>{item.topic}</h4>
<p>{item.title}</p>
</div>
</div>
); )
})}
</Slider>

Try this:
backgroundImage: "url(" + require("../../img/img.png") + ")"
assuming the img.png is in img folder under src (webpack)

Okay, for some reason you can't add styling to the element you are using to house the return content. So here's what you're gonna do:
<Slider {...settings}>
{slides.map((item) => {
return(
<div key={item.id} className="sliderImage">
<div className="backImage" style={{background:`url(/images/covers/${item.cover})`}}>
<div className="slideCaption">
<h4>{item.title}</h4>
<h1>{item.topic}</h1>
</div>
</div>
</div>
)
})}
</Slider>
I hope this helps you solve your problem. ;)

Related

React owl-corusel show only fitted items

I am using owl-corusel in my React Project. I have a problem, the problem is that if an owl-item doesn't fit right on the screen I don't want it to show up. Just center and show how many items appear complete.
Here is my React code :
<OwlCarousel
className="owl-theme"
dots={false}
loop={true}
margin={2}
nav={true}
items={categoryTags.length}
autoWidth={true}
center={true}
>
{categoryTags
? categoryTags.map((item) => {
return (
<div className="item" key={`tag_item_${item.id}`}>
<Link style={{ width: "100%" }} to={"/c/" + item.slug}>
<img
src={item.avatar_url}
alt={item.title}
className="full_height full_height"
/>
<div className="flex align_center cat_ico_hover full_width full_height justify_center">
<i
className="cat_ico"
style={{ backgroundImage: `url(${item.icon_url})` }}
></i>
<span>{item.label}</span>
</div>
</Link>
</div>
);
})
: null}
</OwlCarousel>

How to bring the image to the right of react Card

I have a react Card.
I want to bring the image on the card to the right-side of card and it should also be parellel to the Name Pankaj Rout, and should be responsive.
This is what I have tried so far,
var userMap = [
{name: "Pankaj Rout", email: "pankaj.rout#somedomain.com"}
]
userData.map((data) => {
return (
<div key={data.email}>
<Card style={cardStyle}>
<Card.Body>
<div class="col-sm-6 text-right">
<img
className={classes.img}
style={cardImgStyle}
src="https://www.w3schools.com/howto/img_avatar.png"
alt="sans"
/>
</div>
<Card.Title> {data.name} </Card.Title>
<Card.Subtitle className="mb-2 text-muted">
General Manager
</Card.Subtitle>
<Card.Text> {data.email} </Card.Text>
<Button> Delete </Button>
<Button> Email </Button>
<Button> Chat </Button>
</Card.Body>
</Card>
</div>
);
classes is used from withStyles hook
const useStyles = (theme) => ({
img: {
justifyContent: 'right',
}
});
following style is referring to cardImgStyle
const cardImgStyle = {
width: "30%",
height: "30%",
borderRadius: "50%",
display: "flex",
flexWrap: "wrap",
};
Could anyone please point out what is wrong here?
Thanks!!
justifyContent : 'space-beetwin' ;
or
for img :
float :' right';

How set cards in a row using react bootstrap

I want to add card using react-bootstrap in a project. I used Grid and CardDeck option from react-bootstrap. But all cards comes in a column (like this). But I want this cards comes with side by side, not in row.(I want my output like this image).
Here the code for card section
import Card from 'react-bootstrap/Card';
const Course = (props) => {
const {title, price,details,img} = props;
return (
<div>
<div className="col-md-4 col-12 mt-5 mx-auto">
<Card>
<Card.Img variant="top" src={img} />
<Card.Body>
<Card.Title>{title}</Card.Title>
<Card.Title>{price}</Card.Title>
<Card.Text>
{
details
}
</Card.Text>
</Card.Body>
</Card>
</div>
</div>
);
};
export default Course;
Where I pass the value
import React from "react";
import fakeData from "../FakedData/FakeData";
import Course from "../Course/Course";
import CardDeck from 'react-bootstrap/CardDeck';
const Home = () => {
return (
<div>
<div className="container">
<div className="row">
<CardDeck>
{fakeData.map((data) => {
return (
<Course
key={data.id}
title={data.title}
price={data.price}
details={data.details}
img={data.img}
/>
);
})}
</CardDeck>
</div>
</div>
</div>
);
};
export default Home;
I don't try this, it's the old way with modulo, should work, but not responsive
const Home = () => {
const numInRow = 4;
return (
<div>
<div className="container">
<div class="d-flex justify-content-center"> // <---- div first row
{fakeData.map((data, idx) => { //<--- note the idx
return (
<Course
key={data.id}
title={data.title}
price={data.price}
details={data.details}
img={data.img}
/>
{idx % numInRow === 0 && </div><div class="d-flex justify-content-center">} // <--- use modulo to close row and open a new one
);
})}
</div> //<-- div last row
</div>
</div>
);
};

How to wrapp Reactjs component with CSS

I'm pretty new to Reactjs world and I have wrote component that I would love to make beautiful with CSS :)
I have component consisted of few buttons and boxes which would be displayed after button is clicked.
class Days extends React.Component {
constructor(props) {
super(props);
this.state = { showComponent: "Box1" };
}
toggleDiv = name => {
if (name === "monday") {
this.setState({
showComponent: "Box1"
});
} else if (name === "thursday") {
this.setState({
showComponent: "Box2"
});
}
};
render() {
return (
<div>
<button onClick={() => this.toggleDiv("monday")}>
<div className="botun">Monday</div>
</button>
{this.state.showComponent === "Box1" && <Box1 />}
<button onClick={() => this.toggleDiv("thursday")}>
<div className="botun">Thursday</div>
</button>
{this.state.showComponent === "Box2" && <Box2 />}
</div>
);
}
}
class Box1 extends React.Component {
render() {
return (
<div className="container">
<div className="row">
<div className="col">
<h1>BOx1</h1>
</div>
</div>
</div>
);
}
}
class Box2 extends React.Component {
render() {
return (
<div className="container">
<div className="row">
<div className="col">
<h1>Box2</h1>
</div>
</div>
</div>
);
}
}
After button1 is clicked belonging boxes will be shown under.
So my question is how to put all buttons and boxes into one container and style it like in screenshot?
If I include it in my landingpage.js like this
<div className="container">
<div className="row">
<Days />
</div>
</div>
How can I still make my buttons be in one line?
I am not sure how to approach this with CSS.
What is the best practice when using CSS and CSS frameworks with ReactJS?
I am using global style.css and Boostrap.
You should wrap all your buttons inside a single div, give the container a single class, give it's children a modifier class when they are active and if you want to animate the display of the boxes then you shouldn't unmount them when another button is active opting for making them invisible or something like that because otherwise it's kinda difficult to not have React just pop them into existence.
I use inline style tags. It's really a personal preference situation. As you try new things you'll discover the practice that's best for you and for individual projects. An inline style tag looks like this.<div style={{ display: 'flex', width: '100%', backgroundColor: 'red' }}></div> the benefit to this is you can keep styles defined in the component.

Unable to access data from Meteor in React

When trying to access the root page I get a blank screen with no layout. I check developer tools and get an error in App.jsx
Uncaught TypeError: Cannot read property 'loggedIn' of undefined
This is my App component:
App = React.createClass({
mixin: [ReactMeteorData],
getMeteorData() {
return {
loggedIn: !!Meteor.user()
};
},
showLayout() {
return (
<div className="container-fluid main-container">
<div className="col-xs-3">
{this.props.nav}
</div>
<div className="col-xs-9">
{this.props.content}
</div>
</div>
)
},
showLogin() {
return (
<div className="row">
<div className="col-xs-12 text-center">
<p>You must be logged in to do that.</p>
</div>
<Login />
</div>
)
},
render() {
return(
<div className="container-fluid main-container">
<div className="row">
{ this.data.loggedIn ? this.showLayout() : this.showLogin() }
</div>
</div>
)
}
});
I'm not sure what the issue can be, I'm following along a tutorial.
You should have this:
mixins: [ReactMeteorData],
instead of this:
mixin: [ReactMeteorData],
(Note the 's')

Resources