How to Import Image (.Svg, .Png ) in a React Component

How to import image (.svg, .png ) in a React Component

try using

import mainLogo from'./logoWhite.png';

//then in the render function of Jsx insert the mainLogo variable

class NavBar extends Component {
render() {
return (
<nav className="nav" style={nbStyle}>
<div className="container">
//right below here
<img src={mainLogo} style={nbStyle.logo} alt="fireSpot"/>
</div>
</nav>
);
}
}

How to import image(SVG or PNG) in React using Create React App

Here are three ways to import an image (SVG and PNG) into a React project. You can use either file type with all three options.

  1. Import image and use it in a src attribute.
  2. Import image and use it in a style attribute.
  3. Dynamically insert into a require function.

See examples below:

import React from 'react';
import backgroundImg from '../assets/images/background.png';
import logoImg from '../assets/images/logo.svg';

const Test = props => {

const imageLink = 'another.png'
// const imageLink = props.image
// You can loop this component in it's parent for multiple images.

return (
<div>
<img src={logoImg} style={{ width: '200px', height: '45px' }} />
<div style={{ width: '100%', height: '100%', backgroundImage: `url(${backgroundImg})`, backgroundRepeat: 'no-repeat', backgroundPosition: 'center', backgroundSize: 'cover', position: 'fixed'}} />
<img width={900} height={500} alt="test img" src={require(`../assets/images/${imageLink}`)}/>
</div>
)
}

export default Test

Can I import a png/jpg image like a component? React.js

Yes, you can do it like this:

import yourImage from "./<path_to_png>/image.png"

Then use it like this:

<img src={yourImage} />

How to display svg icons(.svg files) in UI using React Component?

You can directly use .svg extension with img tag if the image is remotely hosted.

ReactDOM.render(
<img src={"http://s.cdpn.io/3/kiwi.svg"}/>,
document.getElementById('root')
);

Here is the fiddle: http://codepen.io/srinivasdamam-1471688843/pen/ZLNYdy?editors=0110

Note: If you are using any web app bundlers (like Webpack) you need to have related file loader.

Error Importing Image into React Component

Try again to export without {}

import ImageJPG from "../images/stock.jpg";

If it is svg

import {ReactComponent as ImageSVG} from "../images/stock.svg"

Importing images in TypeScript React - Cannot find module

If you literally wrote "include": ["./src/index.d.ts"] in tsconfig.json and you don't have a "files" setting, that means the project defined by tsconfig.json includes only the single file ./src/index.d.ts. When you open any other file in VS Code, VS Code uses a separate language service instance that doesn't use your tsconfig.json. Adjust your "include" setting to match all the .ts and .tsx files in your project, or just delete it and rely on the default behavior of including all files under the directory containing tsconfig.json.

Round 2

TypeScript is ignoring index.d.ts because it assumes that index.d.ts is generated from index.tsx and index.tsx is more likely to be up to date. Name your index.d.ts file something else, e.g., declaration.d.ts.



Related Topics



Leave a reply



Submit