简体   繁体   English

React 17.0.1 - 使用动态路径导入组件

[英]React 17.0.1 - Importing Component with Dynamic Path

I am trying to learn how to use "useState" hooks using this guide but I can't seem to import images with a dynamic path, so that when I click a button the element gets a new path and changes the image.我正在尝试学习如何使用本指南使用“useState”挂钩,但我似乎无法导入具有动态路径的图像,因此当我单击按钮时,元素会获得新路径并更改图像。

Here is how the guide is importing images:以下是指南导入图像的方式:

...
function App() {
  const [firstDieResult, setFirstDieResult] = useState(1);
  const [secondDieResult, setSecondDieResult] = useState(6);
  
  const firstDieImage = require(`./assets/${firstDieResult}.png`);
  const secondDieImage = require(`./assets/${secondDieResult}.png`);
...

However, when I try to import images this way it wont import them.但是,当我尝试以这种方式导入图像时,它不会导入它们。 I am importing them as such:我正在这样导入它们:

import React, {useState} from 'react';
import './App.css';
import firstDieImage from './assets/1.png'
import secondDieImage from './assets/2.png'

function App() {
...

But when I import them this way, I can't import a dynamic path (like import firstDieImage from './assets/${firstDieResult}.png' ).但是当我以这种方式导入它们时,我无法导入动态路径(例如import firstDieImage from './assets/${firstDieResult}.png' )。

How do I go about importing images this way?我 go 如何以这种方式导入图像?
Thank you for your time.感谢您的时间。

Whole code for reference:完整代码供参考:

import React, {useState} from 'react';
import './App.css';
import firstDieImage from './assets/1.png'
import secondDieImage from './assets/2.png'

function App() {

  const [firstDieResult, setFirstDieResult] = useState(1)
  const [secondDieResult, setSecondDieResult] = useState(6)
  console.log(secondDieResult)
  
  //Commented out parts work in the guide, but not for me
  //const firstDieImage = require('./assets/1.png')
  //const secondDieImage = require('./assets/2.png')
  //const firstDieImage = require('./assets/${firstDieResult}.png')
  //const secondDieImage = require('./assets/${secondDieResult}.png');

  return (
    <div className="App">
      <header className="App-header">
        <div style={{ display: 'flex', margin: 20 }}>
          <img src={firstDieImage} className="die" alt="Die one" />
          <img src={secondDieImage} className="die" alt="Die two" />
        </div>
        <span>firstDieResult + secondDieResult</span>
        <button className="button">Roll</button>
      </header>
    </div>
  );
}

export default App;

Your code has a mistake.你的代码有错误。

const firstDieImage = require('./assets/${firstDieResult}.png')

You should use backtick character (`) instead of a single quote (').您应该使用反引号 (`) 而不是单引号 (')。 And if you create project using create-react-app module, update the code like this:如果您使用create-react-app模块创建项目,请像这样更新代码:

const firstDieImage = require(`./assets/${firstDieResult}.png`).default

You can find working code from this git repo .您可以从此git 存储库中找到工作代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM