7

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

18
  • You could try backgroundImage instead of background? If you post a jsfiddle I could help more. Commented May 22, 2018 at 1:21
  • @BenLorantfy i'll try posting but im using a json-server Commented May 22, 2018 at 1:23
  • Try opening the generated image url direct in your browser: localhost:3000/images/covers/cover-slug.png, check if the path is correct Commented May 22, 2018 at 1:57
  • @PedroFernandes tried it it's correct Commented May 22, 2018 at 2:09
  • try adding slash before images in url Commented May 22, 2018 at 3:00

7 Answers 7

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Welcome to SO! Please know that answers need explanation.
0

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})`
};

5 Comments

uhh, i'm getting the filename from a table check db.json, inline with the item.id
oh yeah, sorry. I'll keep looking
I think this is the solution: stackoverflow.com/a/30868078/6172657
Can you share your webpack config? I'm guessing that you can get it to work if (1) webpack properly transpiles the files, and (2) you have the correct transpiled path.
where can i find that? sorry i'm quite new to this.
0

enter image description hereBelow 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.

Comments

0

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>

Edit nkrjnwlkoj

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 }}>

9 Comments

i defined it under style.css
yeah no luck. but if i try <img> it does appear.
yes i've check the exact same path. it does appear in <img>
codesandbox.io/s/5v0308l68k here you go, i can't upload images folder says it's unauthenticated?
Module not found: Can't resolve './images/covers' this is what i receive
|
0

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>

2 Comments

uhh, i use map() to access the paths cause i have the filename under my db.json
Yes thats fine just import the files before using via dynamic imports and use the import name to use images or other media
0

Try this:

backgroundImage: "url(" + require("../../img/img.png") + ")"

assuming the img.png is in img folder under src (webpack)

Comments

0

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. ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.