question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Loading a local image using require() doesn't work

See original GitHub issue

Hi,

Is this a bug report?

yes.

Actual Behavior

I’m getting “require() must have a single string literal argument” when I’m trying to require a local image. Here is what I do :

I’m retrieving the image path as a string from Firebase database and I’m setting a state with it :

profilePicture: user.val().picture_1

here is the path => “…/…/img/user/profile/blank_profile.png”

Then I’m trying to set my image source using the path that I just retieved :

<Thumbnail large source={require(this.state.profilePicture)}/>

But I’m getting the error “require() must have a single string literal argument”.

Thank you in advance !

Expected Behavior

The image is displayed.

Steps to Reproduce

Require a local url from firebase stored in a state.

Environment

Environment: OS: Windows 10 Node: 6.10.2 Yarn: Not Found npm: 3.10.10 Watchman: Not Found Xcode: N/A Android Studio: Version 2.3.0.0 AI-162.3871768

Packages: (wanted => installed) react: 16.0.0-beta.5 => 16.0.0-beta.5 react-native: ^0.49.5 => 0.49.5

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
talcoolcatcommented, Jan 17, 2018

Apparently it cannot work - require() is processed during transpilling or whatever you want to call a processing that happens during building the app, hence it requires a constant value. We are using a workaround:

import React, {Component} from 'react';
import {Image} from 'react-native';

class StaticImage extends React.Component {
  constructor(props) {

    super(props);

    this.images = {
      './../assets/img-0.png':require('./../assets/img-0.png'),
      './../assets/img-1.png':require('./../assets/img-1.png'),
      './../assets/img-2.png':require('./../assets/img-2.png'),
      './../assets/img-3.png':require('./../assets/img-3.png'),
      './../assets/img-4.png':require('./../assets/img-4.png'),
      './../assets/img-5.png':require('./../assets/img-5.png')}

      this.actualimage = this.images[props.staticsource]
      console.log("staticsource:",props.staticsource)
  }
  render() {
    return (
      <Image source={this.actualimage} {...this.props}/>
  )}
}
1reaction
lwinkyawmyatcommented, Dec 9, 2017

Hi @packouray, it absolute worked. Can you check on snack?

Read more comments on GitHub >

github_iconTop Results From Across the Web

React won't load local images - Stack Overflow
Best way to load local images in react is as follows. For example, Keep all your images(or any assets like videos, fonts) in...
Read more >
Require Image Not Working in ReactJS - YouTube
While working with ReactJS, any JavaScript library or framework, ... React won't load local images in some cases while using require.
Read more >
How to display images from local assets/images folder when ...
In this article, we will see how we can load local images when using React. ... So to fix this, we need to...
Read more >
React doesn't load local images : r/reactjs - Reddit
Project was created without using npx create-react-app . My App.js import React, { Component } from 'react' class App extends Component { render ......
Read more >
Different Ways to Display Images in React Apps
We can also use the require keyword to load the images into your component. In that case, your code should look like this:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found