Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

React.js react-file-icon Example to Generate & Render SVG Icons of Different File Types in Browser Using Javascript

Posted on February 19, 2023

 

 

Welcome folks today in this blog post we will be using the react-file-icon library to generate and render svg icons of different file types in browser using javascript. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to make a new react.js app using the below command as shown below

 

 

npx create-react-app sampleapp

 

 

cd sampleapp

 

 

And after that you need to install the below library using the below command as shown below

 

 

npm i react-file-icon

 

 

And after that you will see the below directory structure of the react.js app as shown below

 

 

 

 

 

Embeding SVG Icons of Different Files

 

 

Now we can modify the App.js file and copy paste the following code

 

 

App.js

 

 

JavaScript
1
2
3
4
5
6
7
8
9
import { FileIcon, defaultStyles } from "react-file-icon";
 
export default function App() {
  return (
    <div className="App" style={{ height: "80px", width: "80px" }}>
      <FileIcon extension="docx" {...defaultStyles.docx} />
    </div>
  );
}

 

 

As you can see we are importing the react-file-icon library at the top and then we are embeding the FileIcon widget and we are passing the extension attribute which is docx for word document files and also we are passing the defaultstyles for the docx files.

 

 

 

Showing List of All SVG Icons

 

 

App.js

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { FileIcon, defaultStyles } from "react-file-icon";
 
const styledIcons = Object.keys(defaultStyles);
 
export default function App() {
  return (
    <div className="App">
      <div className="icons" style={{display:"flex",flexWrap:"wrap"}}>
        {styledIcons.map((icon) => (
          <div key={icon}  className="icon" style={{width:"80px",margin:"5px"}}>
            <FileIcon extension={icon} {...defaultStyles[icon]} />
          </div>
        ))}
      </div>
    </div>
  );
}

 

 

 

 

Recent Posts

  • Node.js Express Passwordless Login Example to Send Magic Auth Link Using Nodemailer
  • Python 3 Tkinter Script to Encrypt PDF Documents With Password Using PyPDF2 Library GUI Desktop App
  • Node.js Express Project to Upload Image Buffer & BLOB Data to MongoDB Database & Display it in Browser Using Javascript
  • Node.js Tutorial to Export Images to PDF Document With Effects Using FilePix Library in Javascript
  • Node.js Tutorial to Export All Pages of PDF Document to Images and Save it in Javascript
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme