Explain Codes LogoExplain Codes Logo

React JSX: selecting "selected" on selected option

javascript
conditional-rendering
state-management
accessibility
Nikita BarsukovbyNikita Barsukov·Dec 9, 2024
TLDR

To select a default option in a React <select>, you would assign its value to a state variable.

class SelectComponent extends React.Component { state = { selected: 'value2' }; // Set default selected value like boss of the galaxy handleChange = (e) => this.setState({ selected: e.target.value }); // React magic happening here render() { return ( <select value={this.state.selected} onChange={this.handleChange}> <option value="value1">Universe 1</option> <option value="value2">Universe 2</option> {/* The Chosen One */} <option value="value3">Universe 3</option> </select> ); } }

The value of this.state.selected correlates with the option value, ensuring the correct option is selected.

Conditional Rendering: The Art of Dynamics

Rendering of options can dynamically change based on specific conditions. Using conditional rendering in React, the options can be displayed as per the set criteria.

<select value={this.state.selected} onChange={this.handleChange}> {this.state.options.map(option => option.isAvailable ? <option key={option.value} value={option.value}>{option.label}</option> : null )} </select>

Only the options fulfilling the isAvailable condition will be displayed.

State Management: Playing with Multiple Inputs

For efficiently handling complex forms, React's useState hook is a life saver. With a single handleChange method, you can manage multiple selects or inputs.

const [formData, setFormData] = React.useState({ firstSelect: '', secondSelect: '' }); // Classic transformation of all forms into Autobots const handleChange = (e) => { const { name, value } = e.target; setFormData(prevState => ({ ...prevState, [name]: value })); };

Featuring TypeScript here, helps flesh out interface of your state, enhancing type safety.

interface FormState { firstSelect: string; secondSelect: string; // additional fields... }

Enhancing Accessibility: The Label-Select Duo

Boost the usability by pairing <select> with a proper <label>. This not only assists screen reader users but also offers an improved UX.

<label htmlFor="mySelect">Choose an option:</label> <select id="mySelect" value={this.state.selected} onChange={this.handleChange}> {/* options */} </select>

i18n: Bridging Language Gaps

The useTranslation hook from 'react-i18next' can be implemented for localization. This way you can cater to international audiences.

const { t } = useTranslation(); return ( <select value={this.state.selected} onChange={this.handleChange}> <option value="value1">{t('option1')}</option> {/* more options */} </select> );

This will make the select dropdowns adaptable to different languages, thus enhancing UX globally.

Performance Tuning: Power-up Components

For efficient performance, adhere to these best practices:

  • Use arrow functions in render method sparingly to avoid creating a new function each render
  • Event handlers should be binded in the constructor
  • Use of memoization where appropriate can avoid unnecessary renders
  • Implement pure components or React.memo
  • Manage multiple form inputs through a centralized state object and a generic handler