Search

React's Articles

React is a JavaScript library for building user interfaces. It is maintained by Facebook and a community of individual developers and companies. React can be used as a base in the development of single-page or mobile applications.

ReactJs File uploading Example with Axios
This tutorial will provide an example of react js file upload component. you can understand the concept of react js file upload to server. you can optically discern react js upload a file. This post will give you a simple example of react js ajax file upload. Follow the below tutorial step of reactjs file upload formdata. In react upload image with Axios first you have to install Axios in your project. then we got one file input in a form. after that on file input transmute you have to fire one function. in this function, we set the file object in the state. then we fire API with Axios on the form submit button. npm install axios --save /src/App.js file import React from 'react'; import './App.css'; import FileUpload from './FileUpload'; function App() { return ( <div> <FileUpload /> </div> ); } export default App; /src/FileUpload.js file import React from 'react' import axios from 'axios'; class FileUpload extends React.Component{ constructor(){ super(); this.state = { selectedFile:'', } this.handleInputChange = this.handleInputChange.bind(this); } handleInputChange(event) { this.setState({ selectedFile: event.target.files[0], }) } submit(){ const data = new FormData() data.append('file', this.state.selectedFile) console.warn(this.state.selectedFile); let url = "http://localhost:8000/upload.php"; axios.post(url, data, { // receive two parameter endpoint url ,form data }) .then(res => { // then print response status console.warn(res); }) } render(){ return( <div> <div className="row"> <div className="col-md-6 offset-md-3"> <br /><br /> <h3 className="text-white">React File Upload - HackTheStuff</h3> <br /> <div className="form-row"> <div className="form-group col-md-6"> <label className="text-white">Select File :</label> <input type="file" className="form-control" name="upload_file" onChange={this.handleInputChange} /> </div> </div> <div className="form-row"> <div className="col-md-6"> <button type="submit" className="btn btn-dark" onClick={()=>this.submit()}>Save</button> </div> </div> </div> </div> </div> ) } } export default FileUpload; /upload-react/upload.php file header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Methods: PUT, GET, POST"); header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); // Folder Path For Ubuntu // $folderPath = "/var/www/upload-react/"; // Folder Path For Window $folderPath = "upload-react/"; $file_tmp = $_FILES['file']['tmp_name']; $file_ext = strtolower(end(explode('.',$_FILES['file']['name']))); $file = $folderPath . uniqid() . '.'.$file_ext; move_uploaded_file($file_tmp, $file); return json_encode(['status'=>true]); I hope it can help you.
React use one Component to many HTML Fragments
A react component is used to return multiple child component. Some of child component is used at multiple places. Suppose you have a piece of code which you want to reuse at multiple place. Then it is better to create component and reuse it at many times. In this article, I will share with you how you can create a component which will be used at multiple times in React application. Suppose you have a dropdown of whole countries list. You probably don't want to copy-paste whole drop-down all the time. In this condition, it is better to create a component and import it where you need it. Here is the component which you want to create. import React from 'react' const Coutries = () => {     return (     <React.Fragment>       <select name="countries">         <option value="AF">Afghanistan</option>         <option value="AL">Albania</option>         <option value="DZ">Algeria</option>         <option value="AD">Andorra</option>         ...       </select>     <React.Fragment>     )   } } And use below anywhere you want to add in your React application. import Countries from './Countries.js'; function App() {   return (     <>      <Countries></Countries>     </>    ); } export default App; I hope you liked and help in your work.
How to create components in ReactJS
In this article, we will share with you how to create components in React Js. we all know that components are a very basic and small part of code in react js. it's write in different files and import the main file. Why Create a Components in ReactJs? First, things are what are components in react js? the component is a small pease of file code which we can use or import in our main application index.js file. And, the second question is comes to our mind why create components in react js? so, it's the simple answer is to manage the large application's code in easy ways. we can also write all code in index.js file without creating any components in react js. but it is so difficult to understand when the application becomes large. Create a New React App Run the following command in your terminal to create a new fresh React.js application. sudo npx create-react-app helloworldapp --use-npm After done or create React.js application in your local system. application structure looks like bellow. helloworldapp ├── build ├── node_modules ├── public │ ├── favicon.ico │ ├── index.html │ └── manifest.json ├── src │ ├── App.css │ ├── App.js │ ├── App.test.js │ ├── index.css │ ├── index.js │ ├── logo.svg │ └── serviceWorker.js ├── .gitignore ├── package.json └── README.md Create a Demo Component After, create the fresh reactJs application then now we need to create one test component for an example. create the src/DemoComponent.js file and write the following code into this component file. import React from 'react'; const DemoComponent = () => { return ( <div>Hi, i am a component!</div> ); }; export default DemoComponent; Import Component Now, open your src/index.js file and just copy past the following code into it. // import the react and the reactDOM libraries import React from 'react'; import ReactDOM from 'react-dom'; import DemoComponent from './DemoComponent'; // Create react component const App = () => { return ( <div> Hello World, <DemoComponent /> </div> ); }; // Take the react component and show it on the screen ReactDOM.render( <App />, document.querySelector('#root') ); After done changes then how to run it? just run the following command in the terminal. sudo npm start Output : the output will be print in a web browser looks like Hello World, Hi, i am a component! We, hope it can help you.
React JS Axios File Upload Example
In this tutorial, we will provide you example of react js file upload component. You can easily understand a concept of react js file upload to server. This article will give you simple example of react js ajax file upload. Follow the below tutorial step  by step to reactjs file upload formdata. First of all, you have to install axios in your project. npm install axios --save Then we got one file input in form. /src/App.js import React from 'react'; import './App.css'; import FileUpload from './FileUpload'; function App() {   return (     <div>                <FileUpload />     </div>   ); } export default App; After that on file input change you have to fire one function. In this function we set file object in state. Then we fire api with axios on form submit button. /src/FileUpload.js import React from 'react' import axios from 'axios'; class FileUpload extends React.Component{     constructor(){         super();         this.state = {             selectedFile:'',         }         this.handleInputChange = this.handleInputChange.bind(this);     }     handleInputChange(event) {         this.setState({             selectedFile: event.target.files[0],           })     }     submit(){         const data = new FormData()          data.append('file', this.state.selectedFile)         console.warn(this.state.selectedFile);         let url = "http://localhost:8000/upload.php";         axios.post(url, data, { // receive two parameter endpoint url ,form data          })         .then(res => { // then print response status             console.warn(res);         })     }     render(){         return(             <div>                 <div className="row">                     <div className="col-md-6 offset-md-3">                         <br /><br />                             <h3 className="text-white">React File Upload - Nicesnippets.com</h3>                             <br />                             <div className="form-row">                                 <div className="form-group col-md-6">                                     <label className="text-white">Select File :</label>                                     <input type="file" className="form-control" name="upload_file" onChange={this.handleInputChange} />                                 </div>                             </div>                             <div className="form-row">                                 <div className="col-md-6">                                     <button type="submit" className="btn btn-dark" onClick={()=>this.submit()}>Save</button>                                 </div>                             </div>                     </div>                 </div>             </div>         )       } } export default FileUpload; Below is the server code how to save files into server. /uploads/upload.php <?php header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Methods: PUT, GET, POST"); header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");     // Folder Path For Ubuntu // $folderPath = "/var/www/uploads/"; // Folder Path For Window $folderPath = "uploads/"; $file_tmp = $_FILES['file']['tmp_name']; $file_ext = strtolower(end(explode('.',$_FILES['file']['name']))); $file = $folderPath . uniqid() . '.'.$file_ext; move_uploaded_file($file_tmp, $file); return json_encode(['status'=>true]); I hope it will help you.
Read the Current full URL with React
Current url holds data to display or show into view in segment query string. In this article, we will discuss about how to get current page url in react application. In this tutorial, I will show you reactjs get current page url example. Javascript window.location.href property holds the current url which you can use it into React. Let's see below example. import React from 'react' export default class Home extends React.Component{     render() {         const currentUrl = window.location.href;         return(             <p>                 { currentUrl }             </p>         );     } } This way you can get current url into React application. I hope it will help you.
React Bootstrap Navbar Example
Bootstrap is easy to create responsive website with less code. Bootstrap provides easy to add component with simple design. In this tutorial article, I will show you how to add Bootstrap navbar in React application. Bootstrap provides different style and color navbar. In React application we will install react-bootstrap package to use Boostrap navbar components. Run the npm command to install Bootstrap package. npm install react-bootstrap bootstrap Now you will need to add bootstrap CSS file in index.js file. import "bootstrap/dist/css/bootstrap.min.css"; Now create BootstrapNavbar class at /src/BootstrapNavbar.js file. Add the code in file. You need to import all file which you want to add in navbar link. import React from 'react' import {     BrowserRouter as Router,     Switch,     Route,     useParams, } from "react-router-dom"; import { Navbar, Nav, NavDropdown, Button } from 'react-bootstrap' import Home from './Home'; class BootstrapNavbar extends React.Component {     render(){         return(             <div>                 <div className="row">                     <div className="col-md-12">                         <Router>                             <Navbar bg="dark" variant="dark" expand="lg" sticky="top">                                 <Navbar.Brand href="#home">Bootstrap Navbar</Navbar.Brand>                                 <Navbar.Toggle aria-controls="basic-navbar-nav" />                                 <Navbar.Collapse id="basic-navbar-nav">                                     <Nav className="mr-auto">                                         <Nav.Link href="/">Home</Nav.Link>                                     </Nav>                                 </Navbar.Collapse>                             </Navbar>                             <br />                             <Switch>                                 <Route exact path="/">                                     <Home />                                 </Route>                             </Switch>                         </Router>                     </div>                 </div>             </div>         )       } } export default BootstrapNavbar; Now import BootstrapNavbar class to main layout component file. import './App.css'; import BootstrapNavbar from './BootstrapNavbar.js'; function App() {   return (     <div className="App">       <BootstrapNavbar></BootstrapNavbar>       // ...     </div>   ); } export default App; That's it. This way you can import multiple segment to main layout so it remains clean.
React Responsive Animated Navbar Example
In this article, I will share with you how to create a responsive animated in react application and how to built your navbar in your react application. in all applications, we want to make one navbar and there show some important links called a menu. so how to make it in your first react application. here I will show you how to create a simple responsive animated navbar in react application and how to built your navbar functionality in your react application. You can also download all code from GitHub. Preview Create React App First we need to create our react application using the following command in terminal sudo npx create-react-app navbar --use-npm After done or create React.js application in your local system. application structure looks like bellow. navbar ├── node_modules ├── public │ ├── favicon.ico │ ├── index.html │ └── manifest.json ├── src │ ├── App.css │ ├── App.js │ ├── App.test.js │ ├── index.css │ ├── index.js │ ├── logo.svg │ └── serviceWorker.js ├── .gitignore ├── package.json └── README.md make the above files folder structure like that and remove un-necessary files and older. navbar ├── node_modules ├── public │ ├── favicon.ico │ ├── index.html │ └── manifest.json ├── src │ ├── components │ ├── navbar │ ├── Brand.js │ ├── BurgerMenu.js │ ├── CollapseMenu.js │ ├── Navebar.js │ ├── style │ ├── Global.js │ ├── App.css │ ├── App.js │ ├── App.test.js │ ├── index.css │ ├── index.js │ ├── logo.svg │ └── serviceWorker.js ├── .gitignore ├── package.json └── README.md Install-Package after the create one fresh react application then install the following package in your react application's root directory. npm install react-spring styled-components // OR nmp install react-spring nmp install styled-components you can also install packages in both ways. Create Global.js First, we need to create a Global.js file in the following directory path. src/style/Global.js import { createGlobalStyle } from 'styled-components'; const GlobalStyles = createGlobalStyle` @import url('https://fonts.googleapis.com/css?family=Montserrat:400,600&display=swap');; *, *::after, *::before { margin: 0px; padding: 0px; box-sizing: inherit; } html { font-size: 62.5%; } body { box-sizing: border-box; font-family: 'Montserrat', sans-serif; } `; export default GlobalStyles; Create Brand.js src/components/navbar/Brand.js import React from 'react' import styled from "styled-components"; import logo from "../../logo.svg"; const Brand = () => { return ( <Image src={logo} alt="Company Logo" /> ) } export default Brand const Image = styled.img` height: 85%; margin: auto 0; `; Create BurgerMenu.js src/components/navbar/BurgerMenu.js import React from 'react'; import styled from "styled-components"; const Burgermenu = (props) => { return ( <Wrapper onClick={props.handleNavbar}> <div className={ props.navbarState ? "open" : "" }> <span> </span> <span> </span> <span> </span> </div> </Wrapper> ); } export default Burgermenu; const Wrapper = styled.div` position: relative; padding-top: .7rem; cursor: pointer; display: block; & span { background: #fdcb6e; display: block; position: relative; width: 3.5rem; height: .4rem; margin-bottom: .7rem; transition: all ease-in-out 0.2s; } .open span:nth-child(2) { opacity: 0; } .open span:nth-child(3) { transform: rotate(45deg); top: -11px; } .open span:nth-child(1) { transform: rotate(-45deg); top: 11px; } `; Create CollapseMenu.js src/components/navbar/CollapseMenu.js import React from 'react'; import styled from 'styled-components'; import { useSpring, animated } from 'react-spring'; const CollapseMenu = (props) => { const { open } = useSpring({ open: props.navbarState ? 0 : 1 }); if (props.navbarState === true) { return ( <CollapseWrapper style={{ transform: open.interpolate({ range: [0, 0.2, 0.3, 1], output: [0, -20, 0, -200], }).interpolate(openValue => `translate3d(0, ${openValue}px, 0`), }} > <NavLinks> <li><a href="/" onClick={props.handleNavbar}>link n1</a></li> <li><a href="/" onClick={props.handleNavbar}>link n2</a></li> <li><a href="/" onClick={props.handleNavbar}>link n3</a></li> <li><a href="/" onClick={props.handleNavbar}>link n4</a></li> </NavLinks> </CollapseWrapper> ); } return null; }; export default CollapseMenu; const CollapseWrapper = styled(animated.div)` background: #2d3436; position: fixed; top: 4.5rem; left: 0; right: 0; `; const NavLinks = styled.ul` list-style-type: none; padding: 2rem 1rem 2rem 2rem; & li { transition: all 300ms linear 0s; } & a { font-size: 1.4rem; line-height: 2; color: #dfe6e9; text-transform: uppercase; text-decoration: none; cursor: pointer; &:hover { color: #fdcb6e; border-bottom: 1px solid #fdcb6e; } } `; Create Navbar.js src/components/navbar/Navbar.js import React from 'react' import styled from "styled-components"; import { useSpring, animated, config } from "react-spring"; import Brand from "./Brand"; import BurgerMenu from "./BurgerMenu"; import CollapseMenu from "./CollapseMenu"; const Navbar = (props) => { const barAnimation = useSpring({ from: { transform: 'translate3d(0, -10rem, 0)' }, transform: 'translate3d(0, 0, 0)', }); const linkAnimation = useSpring({ from: { transform: 'translate3d(0, 30px, 0)', opacity: 0 }, to: { transform: 'translate3d(0, 0, 0)', opacity: 1 }, delay: 800, config: config.wobbly, }); return ( <> <NavBar style={barAnimation}> <FlexContainer> <Brand /> <NavLinks style={linkAnimation}> <a href="/">link n1</a> <a href="/">link n2</a> <a href="/">link n3</a> <a href="/">link n4</a> </NavLinks> <BurgerWrapper> <BurgerMenu navbarState={props.navbarState} handleNavbar={props.handleNavbar} /> </BurgerWrapper> </FlexContainer> </NavBar> <CollapseMenu navbarState={props.navbarState} handleNavbar={props.handleNavbar} /> </> ) } export default Navbar const NavBar = styled(animated.nav)` position: fixed; width: 100%; top: 0; left: 0; background: #2d3436; z-index: 1; font-size: 1.4rem; `; const FlexContainer = styled.div` max-width: 120rem; display: flex; margin: auto; padding: 0 2rem;; justify-content: space-between; height: 5rem; `; const NavLinks = styled(animated.ul)` justify-self: end; list-style-type: none; margin: auto 0; & a { color: #dfe6e9; text-transform: uppercase; font-weight: 600; border-bottom: 1px solid transparent; margin: 0 1.5rem; transition: all 300ms linear 0s; text-decoration: none; cursor: pointer; &:hover { color: #fdcb6e; border-bottom: 1px solid #fdcb6e; } @media (max-width: 768px) { display: none; } } `; const BurgerWrapper = styled.div` margin: auto 0; @media (min-width: 769px) { display: none; } `; Change in App.js src/App.js import React, { Component } from 'react' import Navbar from "./components/navbar/Navbar"; import GlobalStyle from './style/Global'; class App extends Component { state = { navbarOpen: false } handleNavbar = () => { this.setState({ navbarOpen: !this.state.navbarOpen }); } render() { return ( <> <Navbar navbarState={this.state.navbarOpen} handleNavbar={this.handleNavbar} /> <GlobalStyle /> </> ) } } export default App i hope you like this article. if you want to download code then click here GitHub.
How to Upload files in React Js using Dropzone Js
In this tutorial you are going to learn how create a file dropzone component from scratch using react. We will discover how to open a file selection dialog in react using some nifty tricks. Furthermore, we will learn how to listen for file drop events and use that to build a reusable dropzone component. Setting up a new React project with create-react-app The first thing we do is setting up a new project. We will do so by using the create-react-app-cli. To create a new project, use the following command: create-react-app react-dropzone Preparing the application For this project to look good, I've adjusted the App.js a little bit. It is now rendering the component we are going to create inside of a simple card: import React, { Component } from 'react' import Dropzone from './dropzone/Dropzone' import './App.css' class App extends Component { render() { return ( <div className="App"> <div className="Card"> <Dropzone onFilesAdded={console.log} /> </div> </div> ) } } export default App The corresponding styles look something like this. Feel free to adjust any of this at is just a nice piece to present the dropzone component. .App { text-align: center; background-color: rgb(206, 213, 223); min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: calc(10px + 2vmin); } .Card { background-color: white; padding: 64px; display: flex; align-items: flex-start; justify-content: flex-start; box-shadow: 0 15px 30px 0 rgba(0, 0, 0, 0.11), 0 5px 15px 0 rgba(0, 0, 0, 0.08); box-sizing: border-box; } Creating the Dropzone component Now that we have a nice foundation, it is time to start creating the dropzone component itself. To do so, create a new directory inside of the src-folder. Inside of that directory, create two new files: Dropzone.js and Dropzone.css. Next, create a new react component inside of Dropzone.js called Dropzone. import React, { Component } from 'react' import './Dropzone.css' class Dropzone extends Component { constructor(props) { super(props) } render() { return <p>Dropzone</p> } } Basic structure The HTML-structure of our component will be quite simple. It is just a wrapper div containing an image a span. render() { return ( <div className="Dropzone"> <img alt="upload" className="Icon" src="baseline-cloud_upload-24px.svg" /> <span>Upload Files</span> </div> ); } To make it look like a dropzone, we are defining the CSS class "Dropzone": .Dropzone { height: 200px; width: 200px; background-color: #fff; border: 2px dashed rgb(187, 186, 186); border-radius: 50%; display: flex; align-items: center; justify-content: center; flex-direction: column; font-size: 16px; } For the icon you can use any image you want. Just make sure to place it in the /public directory of your application and pass the filename to the src-attribure above. You can find the icon used here at the material design website. Because the icon is pure black, we are adjusting it's opacity to look nice. .Icon { opacity: 0.3; height: 64px; width: 64px; } How to open a file dialog Now it is time to implement some functionality. First of all, we want the dropzone to open a file-selection-dialog when it is clicked. This dialog as to be provided by the browser (and the operating system) because we don't have access to the file system from a website (which is actually a good thing). The bad news is that there is no JavaScript API to open a file dialog. The only way to open the dialog is by clicking on a HTML-input element with the type-attribute "file". Of course we don't that. We want our dropzone-div to open the dialog when clicked. Fortunately there is a little trick to make this work anyway. We just place the input-element somewhere, make it invisible and then click it using JavaScript. This does look something like this: render() { return ( <div className="Dropzone"> <img alt="upload" className="Icon" src="baseline-cloud_upload-24px.svg" /> <input ref={this.fileInputRef} className="FileInput" type="file" multiple onChange={this.onFilesAdded} /> <span>Upload Files</span> </div> ); } We make the input invisible by using display: none. .FileInput { display: none; } To be able to reference the input element from code we are using a ref. Because we haven't done so already, we need to initialize this.fileInputRef in the components constructor before we can use it. constructor(props) { super(props); this.fileInputRef = React.createRef(); } Click event listener To actually open the file dialog, we need to click on the input element using JavaScript. openFileDialog() { if (this.props.disabled) return; this.fileInputRef.current.click(); } Notice that we only do this if the property disabled is not true. This is a flag we are going to utilize later to disable our dropzone and reject any input. For this method to execute, we need to call it when our dropzone is clicked. To do that, we are adding an onClick listener to the outer div of the component: render() { return ( <div className="Dropzone" onClick={this.openFileDialog} style={{ cursor: this.props.disabled ? "default" : "pointer" }} > ... </div> ) } Notice, that we also set the cursor to "pointer" when the component is not disabled. Furthermore we have to bind the openFileDialog to the component. Otherwise it would loose its this-reference. We do so in the components' constructor. constructor(props) { super(props); this.fileInputRef = React.createRef(); this.openFileDialog = this.openFileDialog.bind(this); } Defining an output property Next, we need to implement the onFilesAdded() we referenced earlier. This method will be called when the file dialog is closed and files have been selected. Because our component does not know what to do with these files, we are passing it to the parent component. We do so by calling a property called onFilesAdded. onFilesAdded(evt) { if (this.props.disabled) return; const files = evt.target.files; if (this.props.onFilesAdded) { const array = this.fileListToArray(files); this.props.onFilesAdded(array); } } But before we can do so, we need to convert the files we received from a FileList to a plain JavaScript array, because that is much easier to work with. To do that, we are defining the fileListToArray method we used in the mehtod above. fileListToArray(list) { const array = []; for (var i = 0; i < list.length; i++) { array.push(list.item(i)); } return array; } Finally, we have to bind the onFilesAdded method to the component because we are using this here. constructor(props) { super(props); this.fileInputRef = React.createRef(); this.openFileDialog = this.openFileDialog.bind(this); this.onFilesAdded = this.onFilesAdded.bind(this); } Handling Drag & Drop Events The dropzone component can now be used to select files by clicking it. But it wouldn't be called a dropzone if we can't drop files onto it, right? Fortunately it does not take much to add that functionality. All we need to do is to add three new methods called onDragOver, onDragLeave and onDrop. Because all of these methods will need access to the components' this, let's go ahead and bind them first: constructor(props) { super(props); this.state = { hightlight: false }; this.fileInputRef = React.createRef(); this.openFileDialog = this.openFileDialog.bind(this); this.onFilesAdded = this.onFilesAdded.bind(this); this.onDragOver = this.onDragOver.bind(this); this.onDragLeave = this.onDragLeave.bind(this); this.onDrop = this.onDrop.bind(this); } Notice that we also introduced a new state variable called "highlight" and set it to false. We will use it to highlight the dropzone when a file is dragged over it. Let's go ahead and register all these methods for their corresponding event: render() { return ( <div className={`Dropzone ${this.state.hightlight ? "Highlight" : ""}`} onDragOver={this.onDragOver} onDragLeave={this.onDragLeave} onDrop={this.onDrop} onClick={this.openFileDialog} style={{ cursor: this.props.disabled ? "default" : "pointer" }} > ... </div> ) } Also, we are applying the CSS class "Highlight" in case the state variable "highlight" is true. Use any color you want! .Highlight { background-color: rgb(188, 185, 236); } Now it is time to implement each method... onDragOver The onDragOver method is quite simple. All we need to is to set the highlight-state to true in case the component is not disabled. onDragOver(evt) { evt.preventDefault(); if (this.props.disabled) return; this.setState({ hightlight: true }); } We also need to call evt.preventDefault to prevent the default behavior of the browser. onDragLeave This mehtod is even simpler. We just set the highlight-state to false. We don't even check if the component is disabled to not get stuck in the highlight-state in case the component is disabled when a file is dragged above it. onDragLeave() { this.setState({ hightlight: false }); } onDrop In case a file is dropped onto the component, we need to notify the parent component about that. We do so by calling the onFilesAdded property. Also, we set the highlight state to false. If the component is disabled we do nothing. onDrop(event) { event.preventDefault(); if (this.props.disabled) return; const files = event.dataTransfer.files; if (this.props.onFilesAdded) { const array = this.fileListToArray(files); this.props.onFilesAdded(array); } this.setState({ hightlight: false }); } The full dropzone component Here is the full code for the dropzone component: import React, { Component } from 'react' import './Dropzone.css' class Dropzone extends Component { constructor(props) { super(props) this.state = { hightlight: false } this.fileInputRef = React.createRef() this.openFileDialog = this.openFileDialog.bind(this) this.onFilesAdded = this.onFilesAdded.bind(this) this.onDragOver = this.onDragOver.bind(this) this.onDragLeave = this.onDragLeave.bind(this) this.onDrop = this.onDrop.bind(this) } openFileDialog() { if (this.props.disabled) return this.fileInputRef.current.click() } onFilesAdded(evt) { if (this.props.disabled) return const files = evt.target.files if (this.props.onFilesAdded) { const array = this.fileListToArray(files) this.props.onFilesAdded(array) } } onDragOver(evt) { evt.preventDefault() if (this.props.disabled) return this.setState({ hightlight: true }) } onDragLeave() { this.setState({ hightlight: false }) } onDrop(event) { event.preventDefault() if (this.props.disabled) return const files = event.dataTransfer.files if (this.props.onFilesAdded) { const array = this.fileListToArray(files) this.props.onFilesAdded(array) } this.setState({ hightlight: false }) } fileListToArray(list) { const array = [] for (var i = 0; i < list.length; i++) { array.push(list.item(i)) } return array } render() { return ( <div className={`Dropzone ${this.state.hightlight ? 'Highlight' : ''}`} onDragOver={this.onDragOver} onDragLeave={this.onDragLeave} onDrop={this.onDrop} onClick={this.openFileDialog} style={{ cursor: this.props.disabled ? 'default' : 'pointer' }} > <input ref={this.fileInputRef} className="FileInput" type="file" multiple onChange={this.onFilesAdded} /> <img alt="upload" className="Icon" src="baseline-cloud_upload-24px.svg" /> <span>Upload Files</span> </div> ) } } export default Dropzone And here is the used stylesheet: .Dropzone { height: 200px; width: 200px; background-color: #fff; border: 2px dashed rgb(187, 186, 186); border-radius: 50%; display: flex; align-items: center; justify-content: center; flex-direction: column; font-size: 16px; } .Highlight { background-color: rgb(188, 185, 236); } .Icon { opacity: 0.3; height: 64px; width: 64px; } .FileInput { display: none; } i hope you like this article.