react 图像识别_无法在React中基于URL查找图像

react 图像识别

If you're new to React and are having trouble accessing images stored locally, you're not alone.

如果您不熟悉React,并且无法访问本地存储的图像,那么您并不孤单。

Imagine you have your images stored in a directory next to a component like this:

想象一下,您的图像存储在这样的组件旁边的目录中:

/src/components- component1- component2
/img- img1- img2

And you're trying to access the images in the /img directory from component2:

而且您正在尝试从component2访问/img目录中的图像:

import React, { Component, useState, useEffect } from 'react';
import { render } from 'react-dom'
import { useTransition, animated, config } from "react-spring";
import imgArr from './images';
import '../App.css';const Slideshow = () => {const [index, set] = useState(0)const transitions = useTransition(imgArr[index], item => item.id, {from: { opacity: 0 },enter: {opacity: 1 },leave: { opacity: 0 },config: config.molasses,})useEffect(() => void setInterval(() => set(state => (state + 1) % 4), 2000), [])return transitions.map(({ item, props, key }) => (<animated.divkey={key}className="bg"style={{ ...props, slideshowContainerStyle}}><img className="img" src={require(item.url)} alt=""/></animated.div>))
}const slideshowContainerStyle = {width: '80%',height: '300px'
}export default Slideshow;

You've tried the paths ../img/img1.jpg and ..img/img1.jpg, but get Error: Cannot find module '<path>' .

您已经尝试了../img/img1.jpg..img/img1.jpg路径,但得到Error: Cannot find module '<path>'

So what's going on here?

那么这是怎么回事?

关于create-react-app (A little about create-react-app)

Like most people, you probably used create-react-app to bootstrap your project.

像大多数人一样,您可能使用了create-react-app来引导您的项目。

In that case, using relative paths can be a bit tricky because create-react-app builds the HTML, CSS, and JavaScript files to an output folder:

在这种情况下,使用相对路径可能会有些棘手,因为create-react-app将HTML,CSS和JavaScript文件构建到输出文件夹中:

/public- index.html- bundle.js- style.css

There are a number of ways to use images with create-react-app, but one of the easiest is to include your images into /public. When your project is built, /public serves as the root directory.

通过create-react-app有多种使用图像的方法,但是最简单的方法之一就是将图像包含在/public 。 构建项目时, /public充当根目录。

You can read more about adding images or other assets in the Create React App docs.

您可以在Create React App文档中阅读有关添加图像或其他资产的更多信息。

汇入图片 (Importing images)

If you took a look at the docs, you'll notice that the code includes import statements to load assets like images and fonts.

如果您看一下文档,您会注意到该代码包含import语句,以加载诸如图像和字体之类的资产。

Importing is useful when your asset, in this case an image, is in the same directory or near the component that uses it, and won't be used anywhere else. For example, if you have an input component with buttons that use SVGs for thumbs up and thumbs down icons.

当您的资产(在这种情况下为映像)位于使用该资产的目录中或该组件附近,并且在其他任何地方都不会使用时,导入很有用。 例如,如果您有一个带有按钮的输入组件,这些按钮使用SVG表示大拇指和大拇指向下的图标。

A bit advantage of using import is that it will throw an error during build time if there's a typo. This sort of checking helps ensure users won't see a broken image that slipped by.

使用import一个优点是,如果有错字,它将在构建期间引发错误。 这种检查有助于确保用户不会看到滑动的破损图像。

翻译自: https://www.freecodecamp.org/news/unable-to-find-images-based-on-url-in-react/

react 图像识别

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390176.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

html单行元素居中显示,多行元素居左显示

有很多的业务需要元素或者文字如果单行&#xff0c;居中显示&#xff0c;如果数据增多&#xff0c;居中显示代码&#xff08;直接复制到编辑器可用&#xff09;&#xff1a;<!DOCTYPE html> <html lang"en"> <head> <meta charset"UTF-8&q…

ML.NET 0.2版增加了集群和新示例

在今年的Build大会上&#xff0c;微软首次发布了ML.NET。ML.NET是开源的、跨平台的以及运行在.NET上的机器学习框架。微软的Ankit Asthana宣布该项目已经完成了第二版的开发。第二版增加了几个新功能&#xff0c;包括名为集群的新机器学习任务&#xff0c;交叉验证和训练-测试&…

如何变得井井有条-来之不易的秘诀来组织您的生活

Because of the changes brought about by COVID-19, many people have had to find healthy and productive ways of working remotely. 由于COVID-19带来的变化&#xff0c;许多人不得不寻找健康有效的远程工作方式。 Some have been sent home and can continue doing thei…

被未知进程占用端口的解决办法

echo off echo 这是用来结束一个未知进程占用端口的批处理可执行文件ipconfig /allnetstat -anoecho 请查看以上信息&#xff0c;输入被占用的端口号:set /p port请输入port:tasklist|findstr %port%echo 请结合上述程序进行输入&#xff0c;请**谨慎输入**set /p program请输入…

怎样在减少数据中心成本的同时不牺牲性能?

2019独角兽企业重金招聘Python工程师标准>>> 导读虽然组织对数据中心提出了更高的要求&#xff0c;但IT管理人员确实有办法在严格的预算内展开工作。如今&#xff0c;组织认为即使性能预期不断提高&#xff0c;其数据中心预算也在缩减。尽管2018年IT支出总体预计增长…

赛普拉斯 12864_如何使用赛普拉斯自动化辅助功能测试

赛普拉斯 12864In my previous post, I covered how to add screenshot testing in Cypress to ensure components dont unintentionally change over time. 在上一篇文章中 &#xff0c;我介绍了如何在赛普拉斯中添加屏幕截图测试&#xff0c;以确保组件不会随时间变化。 Now…

anaconda在win下和在mac下的安装区别

1. 在win下安装anaconda后会提示你选择环境变量&#xff0c;但是建议使用默认。 于是CMD进入终端和使用navigator进入终端不一样&#xff0c;前者会提示无此命令&#xff0c;只能通过navigator进入终端 即使在系统变量变量Path里添加了路径&#xff0c;使用CMD还是不能使用pyth…

fcn从头开始_如何使用Go从头开始构建区块链

fcn从头开始介绍 (Introduction) With Web 3.0 and blockchain becoming more mainstream every day, do you know what blockchain is? Do you know its technical advantages and use-cases?随着Web 3.0和区块链每天变得越来越主流&#xff0c;您知道什么是区块链吗&#x…

java实现无序数组结构

一、数组的2种定义方式 数据类型 [] 数组名称 new 数据类型[数组长度]; 这里 [] 可以放在数组名称的前面&#xff0c;也可以放在数组名称的后面&#xff0c;一般放在名称的前面 数据类型 [] 数组名称 {数组元素1&#xff0c;数组元素2&#xff0c;......} 这种方式声明数组的…

Android App 的主角:Activity

Android App 程序主要由4种类型组成&#xff1a; 1.Activity&#xff08;活动&#xff09;&#xff1a;主要负责屏幕显示画面&#xff0c;并处理与用户的互动。每个Android App至少都会有一个Activity&#xff0c;在程序一启动时显示主画面供用户操作。 2.Service&#xff08;后…

通过构建Paint App学习React Hooks

According to people in the know, React Hooks are hot, hot, hot. In this article, we follow Christian Jensens 14-part tutorial to find out about the basics of this new feature of React. Follow along to find out more! 据知情人士称&#xff0c;React Hooks很热&…

正则表达式 匹配常用手机号 (13、15\17\18开头的十一位手机号)

原文:正则表达式 匹配常用手机号 &#xff08;13、15\17\18开头的十一位手机号&#xff09;^1[3578]\d{9}$ ^1表示以1开头&#xff0c;[3578]表示第二位的数字为3578中的任意一个&#xff0c;\d{9}表示0~9范围内的数字匹配九次,$表示结束&#xff0c;12位以上的数字不匹配。

Npoi导出excel整理(附源码)

前些日子做了一个简单的winform程序&#xff0c;需要导出的功能&#xff0c;刚开始省事直接使用微软的组件&#xff0c;但是导出之后发现效率极其低下&#xff0c;绝对像web那样使用npoi组件&#xff0c;因此简单的进行了整理&#xff0c;包括直接根据DataTable导出excel及Data…

44. 通配符匹配

44. 通配符匹配 给定一个字符串 (s) 和一个字符模式 &#xff0c;实现一个支持 ‘?’ 和 ‘*’ 的通配符匹配。 ? 可以匹配任何单个字符。 * 可以匹配任意字符串&#xff08;包括空字符串&#xff09;。 两个字符串完全匹配才算匹配成功。说明: s 可能为空&#xff0c;且…

递归javascript_使用freeCodeCamp挑战解释了JavaScript中的递归

递归javascriptIn this article I will touch on a few important ideas to help you understand Recursion in JavaScript. I’m not going to give a full definition here, but you can take a look at what Wikipedia has to say. 在本文中&#xff0c;我将介绍一些重要的想…

入库成本与目标成本对比报表中我学到的东西

1、SQL方面&#xff1a; &#xff08;1&#xff09;、用DECODE函数解决除数为零的情况 具体语法&#xff1a; DECODE&#xff08;参数&#xff0c;0&#xff0c;1&#xff0c;参数&#xff09; ->DECODE(TAB1.A8&#xff0c;0&#xff0c;1&#xff0c;TAB1.A8) &#xff08…

J - Borg Maze

J - Borg Maze 思路&#xff1a;bfs最小生成树。#include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define MAXN 110 using namespace std; int fa[MAXN]; struct nond{int x,y,z; }v[MAXN*MAXN]; s…

1095. 山脉数组中查找目标值

1095. 山脉数组中查找目标值 &#xff08;这是一个 交互式问题 &#xff09; 给你一个 山脉数组 mountainArr&#xff0c;请你返回能够使得 mountainArr.get(index) 等于 target 最小 的下标 index 值。 如果不存在这样的下标 index&#xff0c;就请返回 -1。 何为山脉数组…

【小摘抄】关于C++11下 string各类用法(持续更新)

http://blog.csdn.net/autocyz/article/details/42391155 提供了最简单的详解 下列对本人近期开发中的一些心得体会进行摘抄 1.string按照字符进行截取 示例代码&#xff1a; string teststring "#12313#kajlkfdsa";//通讯消息示例&#xff0c;结合string的内置函数…

sql综合练习题

一、表关系 年级表&#xff1a;class_grade create table class_grade(gid int primary key auto_increment,gname varchar(20) not null); insert into class_grade(gname) values(一年级),(二年级),(三年级); 班级表&#xff1a;class create table class(cid int primary ke…