react-native icon indicating copy to clipboard operation
react-native copied to clipboard

[Image + Packager] Recurring require("image!...") issues

Open JonathanMatthey opened this issue 11 years ago • 61 comments

following up on #282 - it still seems to happen to a lot of us. works fine for a while and then out of nowhere image

tried all the suggested solutions

  • updating global react-native 0.3.1
  • updating local react-native 0.3.1
  • node_modules .. packager.sh with --assetsRoot = defined
  • naming imagesets exactly the same as the image filename

a temporary fix is to host your localhost and serve the image through {{ uri: "localhost... myimage.png"}} - that works fine :) but not a great solution

JonathanMatthey avatar Mar 31 '15 02:03 JonathanMatthey

Was just reading http://stackoverflow.com/questions/29308937/trouble-requiring-image-module-in-react-native apparently a recent update reverted the fix :disappointed:

InfamousVague avatar Mar 31 '15 04:03 InfamousVague

sorry for the bad experience. Does restarting the server helps?

This is certainly a stopgap period until we get to a point where the server manages the assets fully and properly. But first I'll make sure this is more reliable.

amasad avatar Mar 31 '15 08:03 amasad

does anyone know which webpack loader(s) need to be used with a react native <Image /> element? I want to be able to write require('image!myImage') and have webpack leave it alone for react native

willpiers avatar Mar 31 '15 14:03 willpiers

@amasad Restarting the server doesn't seem to help. I've tried for a few hours to attempt different methods but had no success.

InfamousVague avatar Mar 31 '15 17:03 InfamousVague

I spend the past one hour to debug this issue. Images are not working for me either. I have tried all the suggestions here.

I think docs can be made more clear about how to required images once the issue is resolved here.

chirag04 avatar Mar 31 '15 18:03 chirag04

The following command (as suggested in #481) worked for me (v0.1.0):

node_modules/react-native/packager/packager.sh --root=./ --assetRoots=./iOS/Images.xcassets

A note in the documentation would be great. Also, this should probably be defaulted somewhere (at least in the boilerplate generated from the cli).

EDIT: Also works on v0.3.1 (latest version at time of writing)

notduncansmith avatar Mar 31 '15 22:03 notduncansmith

@notduncansmith Yup, this issue is fixed in v0.3.1 Is everyone here on this version?

amasad avatar Mar 31 '15 23:03 amasad

@amasad I'm on the latest commit and it's not working, I figured I'd pull from GitHub vs the NPM release since there are a lot of PR's I'm hoping get merged soon :)

Though it's worth noting I have tried 0.3.1 and it does not work there either for me.

Also in OP:

tried all the suggested solutions

updating global react-native 0.3.1
updating local react-native 0.3.1

InfamousVague avatar Mar 31 '15 23:03 InfamousVague

Matt, just to clarify: it did not work when you used the terminal command I posted above?

On Tue, Mar 31, 2015 at 6:10 PM Matt Wisniewski [email protected] wrote:

@amasad https://github.com/amasad I'm on the latest commit and it's not working, I figured I'd pull from GitHub vs the NPM release since there are a lot of PR's I'm hoping get merged soon :)

Though it's worth noting I have tried 0.3.1 and it does not work there either for me.

— Reply to this email directly or view it on GitHub https://github.com/facebook/react-native/issues/521#issuecomment-88279349 .

notduncansmith avatar Mar 31 '15 23:03 notduncansmith

@notduncansmith Not that I remember, I think it gave me a packager error; I'll give it another shot though.

InfamousVague avatar Mar 31 '15 23:03 InfamousVague

@amasad I'm on latest version and it's not working for me.

@notduncansmith I tried your command also. It's still not working.

chirag04 avatar Mar 31 '15 23:03 chirag04

Can confirm it works if you revert to v0.3.1 (I was running the latest commit vs the NPM module) :+1:

Steps I took: (obvious but might help others) Change version in package.json

"react-native": "0.3.1",

Clean up old version

rm -rf node_modules/

Reinstall

npm install

Add image to images.xcassets. Xcode

Add the require code

            <Image
              style={loginStyles.logo}
              source={require('image!tweetbucket')}
              />

Run @notduncansmith's command to override / set assetRoots,

node_modules/react-native/packager/packager.sh --root=./ --assetRoots=./iOS/Images.xcassets

⌘R in xcode

Yay Yay it worked!

I still think this should be considered an issue though since it was apparently working out of the box in previous versions.

Also odd little bug it actually rendered the view twice at one point which was weird but I couldn't reproduce it.

InfamousVague avatar Mar 31 '15 23:03 InfamousVague

Can confirm it works if you revert to v0.3.1;

Do you mean update to v0.3.1? Since it's the latest version.

It'd be really helpful if you can tell me it this work: Can you follow the standard project creation flow in the getting started guide? Add an image and report back if this works?

http://facebook.github.io/react-native/docs/getting-started.html#content

amasad avatar Mar 31 '15 23:03 amasad

@amasad sorry should note I was using commit hash instead of npm hosted version

It'd be really helpful if you can tell me it this work: Can you follow the standard project creation flow in the getting started guide? Add an image and report back if this works?

I tried that before (last night 9PM EST) and without running a special command to define the assets location it does not work, I get a message something similar to could not find module require(image!someimagename) if you are sure it is there restart the PM

InfamousVague avatar Mar 31 '15 23:03 InfamousVague

@amasad I can confirm it works with 0.3.1 and requiring images like this: require('image!something');

For me it doesn't work because i have an image inside a listView. listView data is stored in the app instead of a network call. I want to host the images also in the app.

Doesn't work when i do require('image!' + this.props.image)

chirag04 avatar Mar 31 '15 23:03 chirag04

Doesn't work when i do require('image!' + this.props.image)

@chirag04 I don't belive you should not use concatenation as outlined here:

// BAD
var icon = this.props.active ? 'my-icon-active' : 'my-icon-inactive';
<Image source={require('image!' + icon)} />

http://facebook.github.io/react-native/docs/image.html#static-assets

InfamousVague avatar Mar 31 '15 23:03 InfamousVague

I know thats a bad practice but how do you solve my usecase mentioned above?

chirag04 avatar Mar 31 '15 23:03 chirag04

@chirag04 That I'm not sure about, I read on SO that version 0.3.0 might work. Have you given that version a try?

InfamousVague avatar Mar 31 '15 23:03 InfamousVague

You should use {uri: this.props.image} for non static images

vjeux avatar Mar 31 '15 23:03 vjeux

@chirag04 The idea with static images (images that appear in the iOS asset bundle, i.e. Images.xcassets), is that they're bundled with the application and thus there's a fixed number of possible images. So as outlined in the docs linked above, to use images dynamically in the way you're describing, you'd want to require all the images and store those in an object or something. Then do a lookup by key.

Example:

var images = {
  first: require('image!first'),
  second: require('image!second'),
  // ... etc
};
var image = images[this.props.image];
<Image source={image} />

notduncansmith avatar Mar 31 '15 23:03 notduncansmith