Creating a Dropdown Component in SPFx Using React
In this article, we will explain how the following code demonstrates a dropdown component using React with Fluent UI in a SharePoint Framework (SPFx) web part:
import * as React from 'react';
import { Dropdown, IDropdownOption } from '@fluentui/react';
// Dropdown options
const options: IDropdownOption[] = [
{ key: 'option1', text: 'Option 1' },
{ key: 'option2', text: 'Option 2' },
{ key: 'option3', text: 'Option 3' },
];
export default class DropdownWebPart extends React.Component {
state = { selectedKey: '' }; // Initialize state with an empty selection
// Handle dropdown selection change
private handleChange = (event: React.FormEvent<HTMLDivElement>, option: IDropdownOption): void => {
this.setState({ selectedKey: option.key }); // Update state when an option is selected
};
// Render method to display the dropdown and the selected option
public render(): React.ReactElement {
return (
<div>
<Dropdown
placeholder="Select an option"
options={options}
onChange={this.handleChange} // Pass the handleChange function to handle option selection
/>
<p>Selected: {this.state.selectedKey}</p> {/* Display the selected option */}
</div>
);
}
}
Breakdown of the Code:
- Dropdown Component:
- The
Dropdowncomponent from Fluent UI provides the dropdown interface. It’s initialized with the list of options defined inoptions, which are passed as props to theDropdown.
- State Management:
- The
stateobject has a single propertyselectedKey, which tracks the option selected by the user. this.setState()is used to update the state whenever a new option is selected. This triggers a re-render, showing the updated selected option.
- Event Handling:
- The
handleChangefunction is passed to theDropdownas theonChangeevent handler. It updates the component’s state based on the user’s selection.
- Render Method:
- Inside the
render()function, we display theDropdowncomponent and use a<p>element to show the currently selected option dynamically. - When a user selects an option, the
handleChangemethod is invoked, updating the state and displaying the selection.
Key React Concepts:
- State: React state in SPFx web parts is crucial because it allows dynamic interaction with components. Here, state controls what the user selects in the dropdown, making the UI interactive.
- Component Re-rendering: When
setState()is called in React, the component re-renders, reflecting the updated state and enabling responsive behavior. - Event Handling: By passing the
handleChangefunction to theonChangeevent of theDropdown, we can capture user input and update the state.
Why State is Essential in SPFx:
In SharePoint Framework (SPFx), React state allows components to react to user input and other dynamic changes without manually refreshing the page. This is particularly useful for modern, interactive web parts like dropdowns, forms, or other controls that depend on user actions.
This simple yet powerful approach to managing user inputs through state ensures that SPFx components remain dynamic, modular, and easy to maintain.

Leave a comment