profile

Diary Of A Dev

#4: Build a Folder/File explorer UI in ReactJs.

Published about 2 months ago • 3 min read

Hello Devs,

The feature we are building today is frequently asked in React interviews. But it doesn't test only your React skills, it test your overall JavaScript and basic logic building skills.

So, let's dive into building our fourth frontend feature: [Folder/File explorer UI]

What are we building?

Live preview with code: https://stackblitz.com/edit/vitejs-vite-4bqecn?file=src%2FApp.jsx

Philosophy

The basic philosophy behind building a "Folder/File explorer UI" is as following:

  • List all the folders and files in the root directory.
  • Clicking on folder name should expand to show all the files inside that folder.
  • Clicking on folder again should collapse to hide all the files inside that folder.
  • Clicking on a file would do nothing in our case.

System Design

To mimic a VS code like folder/file structure we will create a JSON object. This object will contain all the folders and their nested folders/files.

Alright, but there is a big question here. How do we identify which entry in JSON object is a folder and which one is a file?

Well, we can keep nested folders/files in an array of object and add a flag `isFoloder`.

Here is our JSON object with all folders and files.

Now we just need to access this JSON and render the folders/files name in UI. So let's do that.

Let the coding begin.

We'll start by fetching JSON data and map over it in `App.jsx`. And at each iteration we will be check if the item is a folder or a file.

If it is a folder we will return `<Folder />` component else `<File />` component is returned.

Now, let's define our `<Folder />` and `<File />` components.

`components/Folder.jsx`

`components/File.jsx`

Awesome, this renders our folders and files on screen. But wait, ideally we should see a folder structure like the following on screen.

---- index.html (file)
---- app (folder)
-------- app.js (file)
-------- src (folder)
------------ main.jsx (file)
------------ utils.js (file)
-------- app.css (file)

But what we see right now is just `index.html` and `app`, but why?

This is where this question becomes a little bit challenging. Can you figure out on you own? Try and then come back.

Ok, let's solve it.

So, basically we are iterating over top level data only in `App.jsx` that's why it only shows top level folder and file names on the screen. How do we iterate over the nested data? Like inside `app` and then inside `src`.

Well, we can manually do it for `app` and `src` but then it would fail if we change our JSON structure. Our solution should be such that it works automatically with any change in JSON structure.

Did you notice that our top level and nested data, both have the same data structure?

That design will allow us to recursively solve this problem. We will use the concept of recursion to solve this.
Recursion means calling a function withing iteself.

Ex:
function recurseEx() {
recurseEx();
}

Inside the `Folder.jsx` we will again map but this time over nested data and check for `isFolder` flag.

If `isFolder` is false `File.jsx` is returned, else `Folder.jsx` is returned from `Folder.jsx` itself.

Notice, the `Folder.jsx` is returned from `Folder.jsx` itself, and that is recursion. With that now all our nested data is taken care of, no matter how deeply they are nested.

There is also an `isExpanded` state added, which just tracks if a folder is in expanded or collapsed state. And based on that the nested data of that folder is shown or hidden.

Now, try changing the JSON structure and our feature will still work. And that is awesome.


P.S:

This feature is not tough to build if you understand recursion. But I also know that recursion is not an easy concept to wrap our head around.

And there are many more concepts like recursion. And That's why I am building a resource which will teach you such programming concepts with visual examples.

And don't worry it is not gonna cost you anything.

I am still working on that resource, and am very close to launch it.

So, stay tuned and join the newsletter if you haven't already.


At last, as always

Reply to this email, if you have any suggestion on which feature should I build next.


Whenever you are ready, I can help you in 2 ways.

  • Ask Me Anything: If you have any question related to web development. Ask me here. I reply to all questions.
  • LinkedIn For Developers: Book 1:1 call with me to turn around your LinkedIn profile and attract better opportunities.

Diary Of A Dev

Become better F.E developer in 10 minutes a week.

I am building one complex frontend feature or solving a complex frontend interview question every week. And breakdown the exact steps, thought process, and system design behind it. Join the newsletter to learn with me.

Read more from Diary Of A Dev

Hello Devs! Welcome back to another Tuesday of building a frontend feature. Today we are solving a very commonly asked Interactive JS feature in an interview setup. [An interactive pie chart]. This one is going to be a quick issue as there isn't many moving parts to this question. What are we building? Live preview with code: https://stackblitz.com/edit/vitejs-vite-psytzb?file=index.html Philosophy Here the task is to create a range slider and a pie chart and bind the `pie chart` value with...

about 1 month ago • 1 min read

Hello Devs, Nowadays, one feature that you will see in almost all sites specially in blogs is theme switching specifically dark mode. And that's what we are building today. [Dark Mode in NextJs page with Tailwind and Shadcn-UI] So, let's dive in to build our third frontend feature. What are we building? Live preview with code: https://stackblitz.com/edit/stackblitz-starters-adalin?file=app%2Fpage.tsx Philosophy. First, let's understand the philosophy and basic system design behind adding dark...

about 2 months ago • 4 min read
password strength indicator screenshot

Howdy Devs, Las week we built an `Adaptive progress bar text`. Despite the complexity of that feature, I am thrilled to see the response from you all on my first issue. Thanks a lot for that. Today we are building a simple "Password Strength Indicator" in React. So, let's dive in to build our second frontend feature. [Password Strength Indicator] What are we building? Live Preview: https://stackblitz.com/edit/vitejs-vite-usrrut?file=src%2FApp.jsx Let the coding began. If you have built...

2 months ago • 2 min read
Share this post