
ReactJS Time-Picker
React itself does not come with a built-in time picker component like it does with other form elements like input fields and checkboxes. However, you can easily implement a time picker in React using various third-party libraries. One popular choice is the react-time-picker
library. Keep in mind that there might be newer libraries available beyond my last update, so I recommend checking the latest options on platforms like npm.
Here’s an example of how you might use the react-time-picker
library:
- Install the library:
You can install the react-time-picker
library using npm or yarn:
npm install react-time-picker
- Use the TimePicker component in your React code:
import React, { useState } from 'react';
import TimePicker from 'react-time-picker';
function App() {
const [selectedTime, setSelectedTime] = useState('10:00');
const handleTimeChange = (newTime) => {
setSelectedTime(newTime);
};
return (
<div className="App">
<h1>Time Picker Example</h1>
<TimePicker
onChange={handleTimeChange}
value={selectedTime}
/>
<p>Selected time: {selectedTime}</p>
</div>
);
}
export default App;
In this example, the TimePicker
component is imported from the react-time-picker
library. It takes in an onChange
handler to update the selected time and a value
prop to display the current time.
Remember to style the TimePicker
component to match the styling of your application.
Please note that the library landscape can change quickly in the world of web development, so I recommend checking for any updates or newer alternatives in case there are more suitable options available now.