

This was done so that we can add attributes to the file input tag from the parent component via props.
#REACT DROPZONE CODE#
In the code above, we are taking that otherProps variable and passing it to the file input tag. any prop other than label, updateFilesCb, maxFileSizeInBytes). This process of sending data from the child to the parent can be further explained at destructuring, we can now add the props like so:Įarlier, we discussed that any props that we don't destructure will be assigned to the otherProps variable (i.e. As a workaround, we will pass a function declared in the parent component and the file upload component will call that function with the files state as an argument. Since React has unidirectional data flow, we cannot easily pass data from the child component (file upload component) to the parent component. “Why do we need use a callback function to send the files state to the parent component?” Thus, for the parent component to also store the uploaded files, we need the file upload component to send it. Typically, the file upload component will be used in a form and when working with forms in React, the component stores the form data in the state. “Why do we need to send the files state to the parent component?” A callback function used for sending the files state to the parent component.For preventing files above the specified size from being uploaded.Determines the label of the component (e.g.The component will have the following props:
#REACT DROPZONE UPDATE#
The useState hook returns a stateful value which is the same as the value passed as the first argument, and a function to update it.įor our purposes, we will need state to keep track of the uploaded files. Since we are creating a functional component and need to use state, we will use the useState hook. Lastly, within the file-upload folder, create 2 new files.

This makes it easier to find the logic and styles for each component.įollowing this convention, create a components folder in the src folder and then a file-upload folder within the components folder.
#REACT DROPZONE INSTALL#
To install them, run npm i styled-components node-sass.Ī good convention for structuring folders and files is to create a components folder that has a folder for each component.

If you are familiar with building React applications, the easiest way to set up a new React project is by using create-react-app. Prerequisite: Node (for installing npm packages)

