Use static Django file in React component

Below I have a very simple example of my reaction structure in a Django application. How can I reference a static file in a React component? I am trying to make a headline in a reaction and wonder if it is possible to display an image from inside the Component.

Logo.jsx

import React from "react"

export default class Logo extends React.Component {
  render() {
    return (
      <a href="{% url 'index:landing_index' %}"><img src="{% static "img/logo.png" %}"></a>
    )
  }
}

NavBarContainer.jsx

import React from "react"
import Logo from "../components/Logo"

export default class NavBarContainer extends React.Component {

  render() {
      return (
          <Logo />
      )   
   } 
}

Navbar.jsx

import React from "react"
import { render } from "react-dom"

import NavBarContainer from "./containers/NavBarContainer"

class NavBar extends React.Component {
  render() {
    return (
      <NavBarContainer />
    )
  }
}

render(<NavBar />, document.getElementById('navbar'))
+4
source share
1 answer

You will need to manually add a static path or send the path through a java script tag variable.

<script>
var static_root = {{ STATIC_URL }}
</script>
0
source

Source: https://habr.com/ru/post/1649310/


All Articles