Skip to content

Array's of images #2545

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
pedropcruz opened this issue Jun 16, 2017 · 10 comments
Closed

Array's of images #2545

pedropcruz opened this issue Jun 16, 2017 · 10 comments

Comments

@pedropcruz
Copy link

Hello people,

Well, my problem is about images. imagine the problem, that in my div parent, only have tags img children or 1 or more than 1. And i put that img src's on a array like:

-- slider.json --

[
     './img/header.jpg',
     './img/header1.jpg'
]

And this is my React component:

import React, { Component } from 'react';
import * as data from "../resources/slider.json";

class FullWidthHeader extends Component{
    render(){
        return(
           <Image />
        );
    }
}

class Image extends FullWidthHeader{

    createImage(props){
        return(<img src={props} alt="imgwow" />);
    }

    createImages(images){
        return images.map(this.createImage);
    }

    render(){
        return(
            <div>
                { this.createImages(data) }
            </div>
        );
    }
}

export default FullWidthHeader;

My problem i think, its for webpack seeing the img src, i cant see the image... How i can resolve this?

Kind Regards,

Pedro Cruz

@themre
Copy link
Contributor

themre commented Jun 16, 2017

put static images in your public/images/ folder and then reference them in React code as <img src={'static/images' + json[i]} />.
if you want WP to manage images, you will need to define them in your react component (import img1 from './img1.jpg'.

@pedropcruz
Copy link
Author

Hello @themre

that doesn't resolve my problem, i'm not putting my files (and i dont needed) put on public folder. I can do all my changes on src, and webpack can put them on public folder. So i think that is not a good solution.

But i had tried, and when the page had render, the img is not on my src/img directory. And can't understand why...

if you need, i can put printscreen of that.

Many Thanks

Pedro Cruz

@Timer
Copy link
Contributor

Timer commented Jun 16, 2017

webpack isn't able to analyze this, but the fix should be relatively simple.

Change slider.json to slider.js, and replace entries like so:

export default [
  require('./img/header.jpg'),
  require('./img/header1.jpg')
]

Change

import * as data from "../resources/slider.json";

to

import data from '../resources/slider';

@bondz
Copy link
Contributor

bondz commented Jun 16, 2017

Your Image component should also extend React.Component and pass the data as props to the Image component instead.

@pedropcruz
Copy link
Author

pedropcruz commented Jun 16, 2017

Hello @Timer and @bondz

Well, i know that's a good solution, but if my app needs json, this bundle can't resolve this? :| i think that is a possible request .

EDIT:

btw, that solution, the result is this:

./src/resources/slider_images.js

Module not found: Can't resolve './src/img/header.jpg' in '/Users/pedropcruz/Documentos/PROJETOS/ekilibrium_website/src/resources'

and @bondz it's exactly the same. When page rendered's the result is the same.

EDIT2:

Sorry wrong path. is not on ./src/img/header.jpg , but yes on ../img/header.jpg :)

But in any case, i prefer with json. If can anyone resolve that i aprecciate that!

@Timer
Copy link
Contributor

Timer commented Jun 16, 2017

It cannot be done with json unless if you use the public folder. Sorry!

I'll close this as resolved, since you said the js solution works (and #2545 (comment) for json).

@Timer Timer closed this as completed Jun 16, 2017
@pedropcruz
Copy link
Author

lol, it's not suposse to use public folder... So, what's the reason to use webpack?

@ArmorDarks
Copy link

ArmorDarks commented Jun 16, 2017

If you this desperately want to keep json, just do this:

import * as data from '../resources/slider.json'

/* This should properly require images with WebPack */
data.forEach((imagePath) => require(imagePath))

/* ... rest of your code */

This is quite crude example, adjust it to your needs, but I hope you've catched the idea.

@pedropcruz
Copy link
Author

thanks for that @ArmorDarks , but cant require the imagePath, can't understand what's the problem... require module not found.

@thezealousfool
Copy link

thezealousfool commented Oct 12, 2017

Hello @Timer and @ArmorDarks ,

I want webpack to manage my images as I want the service worker to cache them.
I have a component Card which will take the name of the image file as a prop and render it inside it. How do I import a file based on props and how to use it as a background-image for a div (I want to have background-size: cover)?

Thank You

@lock lock bot locked and limited conversation to collaborators Jan 21, 2019
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants