PNG images cannot be loaded | NextJS - reactjs

I have a website serving photos in a gallery format that I've created using Next.js. I have all my images stored in /public/photos/ grouped into subfolders, and they are imported where they are needed (see below).
The main page consists of a grid (the tiles component) of individual tiles (the tile component) with a photo background and a title. The photo is imported into tiles.tsx and passed to the tile component as a prop, so I can add more tiles easily.
When I compile this project with npm run dev, I get this error:
error - ./public/photos/astro/astro.png
Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file.
My webpack config is as follows:
const webpack = require('webpack');
const path = require('path');
module.exports = {
module: {
rules: [
{
test: /\.(png|jpe?g|gif)$/i,
use: [
{
loader: 'file-loader',
},
],
},
],
}
};
tiles.tsx is as follows:
import React from "react";
import Tile from "./tile";
import styles from "#styles/tiles.module.css";
import landscape from "../public/photos/landscape/landscape.png";
import astro from "../public/photos/astro/astro.png";
import cgi from "../public/photos/cg/cg.png";
import flowers from "../public/photos/flowers/flowers.png";
function Tiles() {
return(
<div>
<div className={styles.gallery}>
<Tile title="Landscape" photo={landscape} location="landscape"/>
<Tile title="Astro" photo={astro} location="astro"/>
<Tile title='CGI' photo={cgi} location="cgi"/>
<Tile title="Flowers" photo={flowers} location="flowers"/>
</div>
</div>
);
}
export default Tiles;
tile.tsx is as follows:
import styles from '#styles/tile.module.css'
const Tile = (props) => {
return(
<div>
<a className={styles.linkWrapper} href={props.location}>
<div className={styles.imageContainer}>
<img className={styles.image} src={props.photo}/>
<div className={styles.title}>{props.title}</div>
</div>
</a>
</div>
);
}
export default Tile;
I believe I have my webpack correctly configured to load these files. This error only occurs with png images, jpegs and jpgs pose no issue. I have tried to resolve this with next-images, but was unsuccessful.
Any help if appreciated. I am happy to provide extra code if necessary.

Since your pictures are located in the public folder, and to avoid having the extra configuration to load image files, you can simply reference each image by their path in the public folder starting from the base URL (/).
function Tiles() {
return(
<div>
<div className={styles.gallery}>
<Tile title="Landscape" photo="/photos/landscape/landscape.png" location="landscape"/>
<Tile title="Astro" photo="/photos/astro/astro.png" location="astro"/>
<Tile title='CGI' photo="/photos/cg/cg.png" location="cgi"/>
<Tile title="Flowers" photo="/photos/flowers/flowers.png" location="flowers"/>
</div>
</div>
);
}
Check out Static File Serving for more details.

Related

how to load Svg components in remix run application

I have some Svg files which i want to load as react components in remix run app.
for example in create-react-app your could do something like this
import { ReactComponent as Logo } from './logo.svg';
function App() {
return (
<div>
{/* Logo is an actual React component */}
<Logo />
</div>
);
}
is there any similar way in remix-run do achieve the same ?
I created a workflow to convert the icons to react components in development using svgr and npm-watch. For good measure you will probably want to run the icons script before building for production too.
Install dependencies:
npm install -D #svgr/cli #svgr/plugin-svgo #svgr/plugin-jsx #svgr/plugin-prettier npm-watch npm-run-all
Update package.json scripts:
{
//...
"scripts": {
//...
// task to convert icons to components
"icons": "npx #svgr/cli --out-dir app/icons -- ./icons",
// watch task
"icons:watch": "npm-watch icons",
// compile once and start watching for changes
"dev:svg": "run-s icons icons:watch",
// remix dev
"dev:remix": "remix dev",
// run all dev: scripts including `dev:svg` (depending on the remix template you might need to replace the default `dev` script)
"dev": "run-p dev:*"
},
// npm-watch configuration
"watch": {
"icons": {
"patterns": [
"icons"
],
"extensions": "svg",
"quiet": false
}
},
//...
}
The svg icon files are located in the project root in icons/ and the react components are generated in the app/icons/ folder.
svgr.config.js (optional)
module.exports = {
plugins: ["#svgr/plugin-svgo", "#svgr/plugin-jsx", "#svgr/plugin-prettier"],
typescript: true, // optional
};
svgo.config.js
module.exports = {
plugins: [
{
name: "preset-default",
params: {
overrides: {
removeViewBox: false,
},
},
},
],
};
Unfortunately there is no way currently to import an SVG file in your component file directly like in create react app.
And maintainers do not allow currently manipulating the esbuild config to add features like that via plugins (cf. the discussion in this merge request).
If you don't need to style your SVG, you could simply put it in the public/ folder and include it with an img element in your component.
Smaller JS bundles, faster page hydration, and immutable .svg's over the network that are only ever downloaded once. (cf. this message)
function App() {
return (
<div>
<img src="/logo.svg" alt="My logo" />
</div>
);
}
You could also create yourself a JSX component and put your SVG code inside it (with some manual conversion for prop names), or even put your SVG file in a folder, and use an external automatic conversion tool like SVGR to generate optimized JSX component from it with a simple CLI command.
In that case you have a component that you can import like any other component:
import Logo from './Logo.svg';
function App() {
return (
<div>
<Logo />
</div>
);
}
For me, it only worked by creating a component with the SVG tag and then importing it
const HomeLogo = () => {
return (
<div>
<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48">
<g fill="none" fill-rule="evenodd">
<circle cx="24" cy="24" r="24" fill="#FFF"/>
<path fill="#0B0D17" d="M24 0c0 16-8 24-24 24 15.718.114 23.718 8.114 24 24 0-16 8-24 24-24-16 0-24-8-24-24z"/>
</g>
</svg>
</div>
)
}
export default HomeLogo
You can just import them as described in the documentation:
import type { LinksFunction } from "#remix-run/node"; // or "#remix-run/cloudflare"
import styles from "./styles/app.css";
import banner from "./images/banner.jpg";
export const links: LinksFunction = () => {
return [{ rel: "stylesheet", href: styles }];
};
export default function Page() {
return (
<div>
<h1>Some Page</h1>
<img src={banner} />
</div>
);
}

How to properly import images and videos in Next js

I just want to ask how to properly import images and videos in nextjs? My images doesn't load on nested route page, like this -> localhost:3000/about/missionVision It works on single path though, like this -> localhost:3000/about
This is the code of the component with image that doesn't load on nested route/page.
import React, { Fragment } from "react";
const Banner= () => {
return(
<Fragment>
<header className="banner">
<div className="container">
<div className="logo">
<img src="./images/logo.png" alt="our logo"/>
</div>
</div>
</header>
</Fragment>
)
}
export default Banner;
I used to use this format of importing, but there's always an error when I imported videos/mp4 files, plus image doesn't show. ( I migrated cra to next)
import React, { Fragment } from "react";
import logo from "./../../public/images/logo.png";
const Banner= () => {
return(
<Fragment>
<header className="banner">
<div className="container">
<div className="logo">
<img src={ logo }/>
</div>
</div>
</header>
</Fragment>
)
}
export default Banner;
This is my file structure
root folder
components
Banner
-index.js
page
public
images
videos
Thank you!
Edit: This is the error that shows whenever I try to import a video.
Module parse failed: Unexpected character '' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See
https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)
No Need for import public path just try to access from the public path.
<img src={"/images/xyz.svg"} />

Why local svg icons not resolve in react.js projects?

I have local icons, and I add icons in build folder like screenshot below, then I was import icons like that import {ReactComponent as MyIcon} from "build/icons/my-icon.svg";, but still say "Can't resolve 'build/icons/my-icon.svg'", any idea?
Screenshot:
you need to use file-loader
and when you import dont use brackets since its default export just chose the name directly
import myIcon from "build/icons/my-icon.svg";
const App = () => {
return (
<div className="App">
<img src={myIcon} />
</div>
);
}
Svg tag
second option would be to extract the svg tag and put it directly into you component ,
const App = () => {
return (
<div className="App">
<svg .... // copy the content of your .svg
</svg>
</div>
);
}

How to call image into reactjs and airtable

Am using airtable.com helloworld sample code. my logo.jpg is in the same directory as the index.js files
myproject-folder\frontend\index.js
myproject-folder\frontend\logo.png
The airtable app is not using webpack so I could not import the logo image.
I have tried calling the image directly as per code
below but could not get it to work.
<img src={window.location.origin + './logo.jpg'} />
<img src={'./logo.jpg'} />
here is the code
import {
initializeBlock,
useBase,
useRecords,
} from '#airtable/blocks/ui';
import React, { Component } from "react";
class App extends Component {
render() {
return (
<div>
<h2>Hello world from Airtable</h2>
<img src={'./logo.jpg'} />
</p>
</div>
);
}
}
export default App;
If I understand correctly you are using webpack to build your app.
Webpack treat assets as dependencies. As such, you need to import your image and use an appropriate loader to handle the file type of your logo.
From webpack's documentation:
[...] When you import MyImage from './my-image.png', that image will
be processed and added to your output directory and the MyImage
variable will contain the final url of that image after processing.
[...] The loader will recognize this is a local file, and replace the
'./my-image.png' path with the final path to the image in your output
directory.
In your case, you could do this:
import React, { Component } from "react";
import {
initializeBlock,
useBase,
useRecords,
} from '#airtable/blocks/ui';
import logo from './logo.png';
class App extends Component {
render() {
return (
<div>
<h2>Hello world from Airtable</h2>
<img src={logo} />
</div>
);
}
}
export default App;
You'd then need to add the file-loader to your webpack config file:
{
test: /\.(png|svg|jpg|gif)$/,
use: [
'file-loader',
],
},

React js Import image issue

Help me to solve React js Import image issue the image is not working I try too many times console log error images and folder structure.
If I put direct server URL the images is visible
import React, {Component} from 'react';
import '../assets/css/bootstrap.css';
import '../assets/css/home.css';
import { Meteor } from 'meteor/meteor';
import { render } from 'react-dom';
import logo from '../assets/img/donut-logo.jpg';
class Home extends Component {
render() {
return (
<main>
<div className="container-fluid mainheader">
<div className="container mainheader-wrapper">
<img src="{logo}" />
</div>
</div>
<div className="container mainheader-btn">
<div className="col-md-6">
<a href={FlowRouter.url('login')}>Login</a>
</div>
<div className="col-md-6">
<a href={FlowRouter.url('register')}>Register</a>
</div>
</div>
</main>
)
}
}
Home.propTypes = {};
Home.defaultProps = {};
export default Home;
[![enter image description here][3]][3]
[![enter image description here][1]][1]
[![enter image description here][2]][2]
[1]: https://i.stack.imgur.com/onDQu.png
[2]: https://i.stack.imgur.com/ripwM.png
[3]: https://i.stack.imgur.com/E9TEP.png
Are you using web pack for bundling.
If you used then please follow the below thing.
Add url-loader in the config file of your webpack as follows.
{
test: /\.(png|woff|woff2|eot|ttf|svg)$/,
loader: 'url-loader?limit=100000',
options: {
mimetype: 'image/png'
}
}
Seems like you are using Meteor with ReactJS.
Try to create a public folder in the root directory (same level as client, server, import), place your image in the public folder and then try to import it.

Resources