1

I am trying to load dynamic content to a child component. I am able to render everything except for an img reference. I have tried changing the reference location inside the data.json. Here's the structure:

JSON Structure

[
{
    "name": "Mercury",
    "overview": {
      "content": "Mercury is the smallest planet in the Solar System and the closest to the Sun. Its orbit around the Sun takes 87.97 Earth days, the shortest of all the Sun's planets. Mercury is one of four terrestrial planets in the Solar System, and is a rocky body like Earth.",
      "source": "https://en.wikipedia.org/wiki/Mercury_(planet)"
    },
    "structure": {
      "content": "Mercury appears to have a solid silicate crust and mantle overlying a solid, iron sulfide outer core layer, a deeper liquid core layer, and a solid inner core. The planet's density is the second highest in the Solar System at 5.427 g/cm3 , only slightly less than Earth's density.",
      "source": "https://en.wikipedia.org/wiki/Mercury_(planet)#Internal_structure"
    },
    "geology": {
      "content": "Mercury's surface is similar in appearance to that of the Moon, showing extensive mare-like plains and heavy cratering, indicating that it has been geologically inactive for billions of years. It is more heterogeneous than either Mars's or the Moon’s.",
      "source": "https://en.wikipedia.org/wiki/Mercury_(planet)#Surface_geology"
    },
    "rotation": "58.6 Days",
    "revolution": "87.97 Days",
    "radius": "2,439.7 KM",
    "temperature": "430°c",
    "images": {
      "planet": "./assets/planet-mercury.svg",
      "internal": "./assets/planet-mercury-internal.svg",
      "geology": "./assets/geology-mercury.png"
    }
  }
]

This is the file tree

Screenshot of folder structure
Screenshot of folder structure

The data is within db, and the img is located within MobileView

This is the component in question

import React from "react";

function MobileView({ content }) {
  return (
    <div>
      <img src={content.images.planet} alt=""/>
      <h2>{content.name}</h2>

      {console.log(content.images.planet)}
    </div>
  );
}

export default MobileView;

Kuldeep J
  • 382
  • 5
  • 12

2 Answers2

0

URIs such as "./assets/planet-mercury.svg" found in your example, are not the actual paths once you bundle your application. Thats excludes paths from the public folder.

You can get more context in CRA docs.

Therefore, you need to use dedicated API such as require.

<img src={require(content.images.planet)} alt=""/>
Dennis Vash
  • 50,196
  • 9
  • 100
  • 118
  • I tried this, but it's giving me an error `cannot find module`. I tried changing the ref location in the `data.json` but it's still giving me an error. –  May 02 '22 at 13:03
  • Just create a reproducible example [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Dennis Vash May 02 '22 at 13:05
0
import React, { Component } from "react";
import eventBus from "./eventBus";

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      user: {
        id: 12,
        firstName: "John",
        lastName: "Doe",
      },
    };
  }

  componentDidMount() {
    eventBus.dispatch("userData", this.state.user);
  }

  render() {
    return (
      <div>
        <Account />
      </div>
    );
  }
}

class Account extends Component {
  constructor(props) {
    super(props);
    this.state = {
      user: {},
    };
  }

  componentDidMount() {
    eventBus.on("userData", (user) => {
      this.setState({ user });
    });
  }

  render() {
    const { user } = this.state; // same as -> const user = props.user;
    return (
      <div>
        <span>
          Name - {user.firstName} {user.lastName}
        </spaenter code heren>
        <span>ID - {user.id}</span>
      </div>
    );
  }
}
  • Welcome to Stack Overflow. Code is a lot more helpful when it is accompanied by an explanation. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please [edit] your answer and explain how it answers the specific question being asked. See [answer]. – ChrisGPT was on strike May 13 '22 at 18:14