Tutorials on Dropdown

Learn about Dropdown from fellow newline community members!

  • React
  • Angular
  • Vue
  • Svelte
  • NextJS
  • Redux
  • Apollo
  • Storybook
  • D3
  • Testing Library
  • JavaScript
  • TypeScript
  • Node.js
  • Deno
  • Rust
  • Python
  • GraphQL
  • React
  • Angular
  • Vue
  • Svelte
  • NextJS
  • Redux
  • Apollo
  • Storybook
  • D3
  • Testing Library
  • JavaScript
  • TypeScript
  • Node.js
  • Deno
  • Rust
  • Python
  • GraphQL

React Dropdown Tutorial for Beginners: Create a Dropdown Menu from Scratch

In this hands-on tutorial, I’ll show you how to build a React dropdown menu from scratch, without any library. We’ll cover: how to get the selected value from the dropdown, and how to create the dropdown with both functional and class components.React comes with a lot of component libraries for virtually everything you need, from forms and autocomplete search boxes to sliders and navbars. Although these UI libraries can speed up your work and are pretty convenient to use, they can make your project more complex and add to the bundle size. So unless you need to use a library for a specific project, it's preferable to write your own components and keep them as simple as possible. In this article, we'll create a basic React dropdown menu, first as a functional component, then as a class component. We won't use any library; instead, we'll define the elements of the dropdown using styled-components. By the end of this post, you will know how to create a basic React dropdown menu using functional and class components. Although this is a beginner tutorial, I won't cover the basics of React, so in order to get the most out of this article you should know: Before we start, let’s initiate a new code sandbox with React and install styled-components as a dependency. We’ll remove the styles.css file, as we won’t use it, and we’ll create a new styles.js  file instead, in the src folder, where we’ll build our styled-components.  Also, let’s create the Dropdown.js file where we’ll develop the component. Your project structure should look like this:  Before we dive into the actual component, here's what we’ll cover: You can find the final project here: React dropdown - exercise .  To create the dropdown, we’ll use the HTML <select> element. This has the following structure:  So we have the dropdown element itself (the <select> tag), and the dropdown values (the <option> tags). Next to these, we also want to display a heading on top of the dropdown, to tell the users what this list is for. So we’ll need to add a <label> tag.  Finally, if we want to send the selected options to an API endpoint, we need to wrap this entire component in a <form> tag and to add a button, using the <input type=”submit”> element.  So let’s create these components as styled-components first, in the styles.js file. First, we'll create the wrapper, which is an HTML form. Next, we style the <select> tag and its children. Finally, we style the button. Next, let's import the components and create the dropdown component skeleton in the Dropdown.js file. We have our dropdown component, so let's include it in the App.js file: Right now, your app should look like this. If you select one of the options in the dropdown menu, nothing happens yet. To be able to get the value of the selected option and send it to an API endpoint, we need to add a hook. In the App.js file, adjust the code as follows: Here's what we did. First, we declare a new state variable, which is called optionValue . We want this to be equal to the value selected from the list, so we'll add a function called setOptionValue , which will update the state variable to our selected value. To make this work, we need to add an onChange event handler to the Dropdown component, and to define the function that will be called. So now whenever the value of the dropdown changes, the handleSelect function will be called, and the state value will be updated to the one selected from the list. To test if this works correctly, I've added a <p> tag right after the dropdown, where we'll print the state value. All looks good, so now the only thing that we still need to adjust is the endpoint where we send the selected value. I'll add a fake API endpoint, just to test if the form is submitted correctly. In the Dropdown component, add this path to the action : That's all! Now let's create the same dropdown list using a class component. We'll create a separate project for this, to keep things organized. You can find the code for the dropdown class component here . We'll reuse the same styles, so let's first add styled-components as a dependency. Then, let's create a styles.js file, and copy-paste the previous styles. Next, delete the styles.css file, as we won't use it, and add a Dropdown.js file, for our class component. Copy-paste the code from the functional component - we'll start with that and refactor as needed. Here's the starting code: We'll do the same for the App.js file. Here's the starting code: Let's remove the useState hook as we won't use it. Instead, we'll initiate the state in a constructor, as follows: Now we need to add a render() to the component, to output our dropdown menu. The rest of the component is the same as before, but we've added this to the onChange handler as well as to the selected value. Now we need to refactor the handleSelect function as well, and to add it to the constructor. We've also added an export at the end, so now if we reload the app, we should see exactly the same form as before: As you can see, creating your own React dropdown menu, selecting a value from the list, and sending it to a backend API endpoint is pretty straight-forward, regardless of which type of component you choose: functional or class component. If you'd like to learn more, check out these React tutorials:

Thumbnail Image of Tutorial React Dropdown Tutorial for Beginners: Create a Dropdown Menu from Scratch

Quick & Easy React Dropdown Component

In this post, we will discuss how to build a dropdown component in React. We will also add an animation to our dropdown.There are many approaches you can take when adding a component to your application, there is an abundance of component libraries you can use such as material-ui but sometimes it is better to just get your hands dirty and build your own one! In this post, we are going to do exactly that and build our own dropdown component. A dropdown is a staple in any web application and is most commonly used in menus to display a list of options to the user. A dropdown can also be used to display some information to the user that can be shown on a button click. They are used all across the web and you could also be asked to build one in your next interview! I know that building a component such as a dropdown may seem intimidating, but in this post, we will take it step by step so you can follow along. The knowledge gained from this post will also help you build any custom components! Ready? Let's go! By the end of this post, you will be able to: Armed with the above knowledge, you will have a newfound confidence when approaching building components in React. To get the most out of this article, we assume you're familiar with component basics in React, React Hooks, and also core JavaScript. If you do not know any of the above but would like to learn, here are some great resources: Now it's time to get started on our journey of building our very own dropdown component. Before building our dropdown component, it is important to discuss the different parts of a dropdown. There are four key components to a menu dropdown: Visually, we can imagine it like this: The component structure could look like this: The core functionality would be: The above is just theory, you can be as creative as you want when building your own component. When building custom components, it is really important to build the component to suit your use case as it may be quite different from the standard. It's good to use the best practice guidelines but also don't be afraid to be creative! Let's first start with a very basic dropdown, we will click on a button that will show a pop up that tells the user something. When building a basic dropdown component, there is one important piece of state. The important piece of state is whether the dropdown is open or not. We will also need to add a function to handle toggling this piece of state. This will all live in our wrapper component, which in our case is App . We can use useState to manage the showing and hiding of the dropdown for us (check out the docs here for React hooks ). We will pass both showDropdown and setShowdropdown as props to our component. Now we need to build our BasicDropdown component. The main thing we need is something to trigger our dropdown and also the content of our dropdown. We are using a button to trigger the dropdown and then we only show our content when showDropdown is true. There is a little CSS with this too, the key part of the CSS is setting position:relative on the dropdown-wrapper and position:absolute on the dropdown . We do this so that the dropdown will always be positioned to the the dropdown-wrapper . Visually, we now have: That is it, not too bad, eh? This is a very basic dropdown but still is very commonly used when displaying some information to a user. You could reuse this in various places and even expand it by opening it up to additional props such as a custom color or custom text. Why not try to add those additional props to the Codesandbox below, this will help further your knowledge of building custom components and making them reusable. So we can use a lot of the knowledge we gained above to help build our dynamic dropdown. The big difference with this dropdown is that it will accept a prop named items and it will also have a transition when it is being open/closed. The items prop will determine when it is rendered within our dropdown list. This could literally be anything. A very common scenario is sub-items for a navigation menu. In our case we are going to make up some items: We can then pass this into our dropdown component: Our dropdown component will behave quite similar to the one before, except we will map over each item in our array and create an li item. We will initially set our ul to have height 0 so that it is hidden. When showDropdown is true, we will set the height to 100px which will make it visible. To achieve this, we will use the following CSS (nothing too fancy going on, mostly overriding default browser styles): As you can see from above, we have set a transition on our height so that when the height changes it will do it with an ease transition which will last 300ms . You don't need to always use the animation property. In simple cases where only one attribute is changing in a predictable manner, we can use transition . Visually, we now have this: One important part to note is the overflow:auto . As we get a lot of items being passed to our list, we can then scroll our dropdown to suit this. Complete sandbox. Today we learned: If you're interested in learning further, try to build your own components. There are some great long-form articles you can read on building your own components such as this one . Personally, I would say that if you have read all the above then you should just open your own Codesandbox and start building components such as an Alert or a Modal. This is truly the best way to learn! Have a great day and happy coding.

Thumbnail Image of Tutorial Quick & Easy React Dropdown Component

I got a job offer, thanks in a big part to your teaching. They sent a test as part of the interview process, and this was a huge help to implement my own Node server.

This has been a really good investment!

Advance your career with newline Pro.

Only $30 per month for unlimited access to over 60+ books, guides and courses!

Learn More