Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

image require variable problem #7015

Closed
ibeeger opened this issue Apr 16, 2016 · 15 comments
Closed

image require variable problem #7015

ibeeger opened this issue Apr 16, 2016 · 15 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@ibeeger
Copy link

ibeeger commented Apr 16, 2016

mycode

renderItem(item,i,_this){
    var text = _this.state.names[i]
    var pic = require(item) || require("../../assets/list.png");
        console.log(pic+"k"+item)
      return(
          < TouchableHighlight style={styles.touch} key={i} onPress={this.props.changePage} underlayColor="#c00">
            < View style={styles.cell}>
            < Image
             style={styles.icon}
             source={pic}
           / >
           < Text style={styles.text}>{text}
            
            
        )
  }
var rows = []
        icons.forEach(function (item,i) {
          rows.push(_this.renderItem(item,i,_this));
        })

errcode

function requireImpl(id) {
  if (global.ErrorUtils && !inGuard) {
    inGuard = true;
    var returnValue;
    try {
      returnValue = requireImpl.apply(this, arguments);
    } catch (e) {
      global.ErrorUtils.reportFatalError(e);
    }
    inGuard = false;
    return returnValue;
  }

  var mod = modules[id];
  if (!mod) {
    var msg = 'Requiring unknown module "' + id + '"';
    if (__DEV__) {
      msg += '. If you are sure the module is there, try restarting the packager or running "npm install".';
    }
    throw new Error(msg);
  }
@satya164
Copy link
Contributor

var pic = require(item) || require("../../assets/list.png");

You cannot use variables in require. All requires must be statically analyzable. That means you always have to write require('/path/to/file').

@ibeeger
Copy link
Author

ibeeger commented Apr 17, 2016

@satya164 my assets images in array ,I want to use the array elements require

@satya164
Copy link
Contributor

@ibeeger have the requires in an array instead. e.g. instead of [ 'img1', 'img2' ], you can have [ require('img1'), require ('img2') ]

@ibeeger
Copy link
Author

ibeeger commented Apr 18, 2016

@satya164 thank you

@pppluto
Copy link

pppluto commented May 5, 2016

any other solution if i want require regular image based on some variable ? it costs much time if we have too many image.

@satya164
Copy link
Contributor

satya164 commented May 5, 2016

@MrPluto Packager needs to be able to statically analyze the images to be able to bundle them into the app automatically. As an alternative, you can bundle the images in your app manually and the use <Image source={{ uri: 'file:///path/to/image' }} /> where variables will work.

@pppluto
Copy link

pppluto commented May 5, 2016

@satya164 thanks a lot 👍

@gilnuy
Copy link

gilnuy commented May 6, 2016

hi,all
i have the same problem,how to get the app bundle path with js code?

@dushaobindoudou
Copy link

maybe help requireAll

@amorenew
Copy link

I found this answer useful

const images = {
    profile: {
        profile: require('./profile/profile.png'),
        comments: require('./profile/comments.png'),
    },
    image1: require('./image1.jpg'),
    image2: require('./image2.jpg'),
};

export default images;
import Images from './img/index';

render() {
    <Image source={Images.profile.comments} />
}

@ghost
Copy link

ghost commented Feb 5, 2018

Hi , please who can help me ? I have the same problem , the solutio of @satya164 is useful but in my case I want to register an image in data base , so I must use an variable inside require . Someone have an idea ?

@amorenew
Copy link

amorenew commented Feb 6, 2018

@kilaniba search about base64 images may could solve your problem

@ghost
Copy link

ghost commented Feb 6, 2018

@amorenew You can't more explication ?

@ghost
Copy link

ghost commented Feb 6, 2018

@amorenew Thank you very much .barak allah fik.

@facebook facebook locked as resolved and limited conversation to collaborators May 24, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

7 participants