The Temperature Converter Application is a web app built with React and Redux. It allows users to convert temperatures between Celsius, Fahrenheit, and Kelvin. The app uses Redux for state management to handle temperature conversion logic.
- Convert temperatures between Celsius, Fahrenheit, and Kelvin.
- State management with Redux.
- Responsive design with Tailwind CSS.
- React: Front-end library for building the user interface.
- Redux: State management library.
- Tailwind CSS: Utility-first CSS framework for styling.
This code sets up a Redux slice to handle temperature conversions between Celsius, Fahrenheit, and Kelvin. It uses the @reduxjs/toolkit
library to create a slice of the Redux store for managing temperature conversion state.
import { createSlice } from '@reduxjs/toolkit';
createSlice
is imported from@reduxjs/toolkit
to simplify the creation of Redux slices.
const fahrenheitToCelsius = (temp) => (temp - 32) * 5 / 9;
const kelvinToCelsius = (temp) => temp - 273.15;
const celsiusToFahrenheit = (temp) => (temp * 9 / 5) + 32;
const celsiusToKelvin = (temp) => temp + 273.15;
- These functions convert temperatures between different units:
fahrenheitToCelsius
: Converts Fahrenheit to Celsius.kelvinToCelsius
: Converts Kelvin to Celsius.celsiusToFahrenheit
: Converts Celsius to Fahrenheit.celsiusToKelvin
: Converts Celsius to Kelvin.
const initialState = {
temperature: '',
convertedTemperature: '',
fromUnit: '',
toUnit: '',
};
initialState
defines the initial state of the slice with the following properties:temperature
: The input temperature value.convertedTemperature
: The result of the temperature conversion.fromUnit
: The unit of the input temperature.toUnit
: The unit to which the temperature should be converted.
export const tempSlice = createSlice({
name: 'temp_converter',
initialState,
reducers: {
convertTemperature: (state, action) => {
const { temperature, fromUnit, toUnit } = action.payload;
let tempInCelsius;
switch (fromUnit) {
case 'Fahrenheit':
tempInCelsius = fahrenheitToCelsius(temperature);
break;
case 'Kelvin':
tempInCelsius = kelvinToCelsius(temperature);
break;
case 'Celsius':
default:
tempInCelsius = temperature;
break;
}
switch (toUnit) {
case 'Fahrenheit':
state.convertedTemperature = celsiusToFahrenheit(tempInCelsius);
break;
case 'Kelvin':
state.convertedTemperature = celsiusToKelvin(tempInCelsius);
break;
case 'Celsius':
default:
state.convertedTemperature = tempInCelsius;
break;
}
state.fromUnit = fromUnit;
state.toUnit = toUnit;
},
},
});
tempSlice
is created usingcreateSlice
with the following configuration:name
: The name of the slice,temp_converter
.initialState
: The initial state defined earlier.reducers
: Contains theconvertTemperature
reducer function to handle temperature conversion:- It extracts
temperature
,fromUnit
, andtoUnit
from theaction.payload
. - Converts the temperature to Celsius if needed based on
fromUnit
. - Converts the Celsius temperature to the desired
toUnit
and updatesstate.convertedTemperature
. - Updates
state.fromUnit
andstate.toUnit
with the provided units.
- It extracts
export const { convertTemperature } = tempSlice.actions;
export default tempSlice.reducer;
convertTemperature
is exported as an action creator.- The reducer function of the slice is exported as the default export, to be used in the Redux store.
This code sets up the Redux store using the @reduxjs/toolkit
library and integrates the temperature conversion slice reducer into the store.
import { configureStore } from '@reduxjs/toolkit';
import tempReducer from './features/tempconverter/tempconverSlice'; // Ensure default import
configureStore
is imported from@reduxjs/toolkit
to create and configure the Redux store.tempReducer
is imported from thetempconverSlice
file, which contains the reducer function for the temperature conversion slice. This import is a default import.
export const storetemp = configureStore({
reducer: {
temp_converter: tempReducer,
},
});
storetemp
is created usingconfigureStore
with the following configuration:reducer
: An object that maps the slice name to its corresponding reducer function:temp_converter
: This key in the state object corresponds to the temperature conversion slice. It usestempReducer
as its reducer function, which handles state changes related to temperature conversion.
Here's a brief explanation of each snippet in the TempConverter
component code:
import React, { useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { convertTemperature } from '../app/features/tempconverter/tempconverSlice';
- Imports React and hooks (
useState
). - Imports Redux hooks (
useDispatch
,useSelector
) to interact with the Redux store. - Imports the
convertTemperature
action from the slice.
const [temperature, setTemperature] = useState('');
const [fromUnit, setFromUnit] = useState('');
const [toUnit, setToUnit] = useState('');
const dispatch = useDispatch();
const convertedTemperature = useSelector(state => state.temp_converter.convertedTemperature);
- Manages local state for
temperature
,fromUnit
, andtoUnit
. - Uses
useDispatch
to dispatch actions. - Uses
useSelector
to access theconvertedTemperature
from the Redux store.
const handleConvert = () => {
const tempValue = parseFloat(temperature);
dispatch(convertTemperature({
temperature: tempValue,
fromUnit,
toUnit,
}));
};
- Defines
handleConvert
to parse the temperature input and dispatch theconvertTemperature
action with the current state values.
const isConvertDisabled = !temperature || !fromUnit || !toUnit;
- Checks if the conversion button should be disabled based on whether all required inputs are filled.
return (
<div className="w-full max-w-sm mx-auto">
<input
type="number"
value={temperature}
onChange={(e) => setTemperature(e.target.value)}
placeholder="Enter temperature"
className="mb-4 px-4 py-3 w-full border border-gray-300 bg-gray-50 rounded-lg shadow-sm text-lg focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500"
/>
<select
value={fromUnit}
onChange={(e) => setFromUnit(e.target.value)}
className="mb-4 px-4 py-3 w-full border border-gray-300 bg-gray-50 rounded-lg shadow-sm text-lg focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500"
>
<option value="" disabled>Select from unit</option>
<option value="Celsius">Celsius</option>
<option value="Fahrenheit">Fahrenheit</option>
<option value="Kelvin">Kelvin</option>
</select>
<select
value={toUnit}
onChange={(e) => setToUnit(e.target.value)}
className="mb-6 px-4 py-3 w-full border border-gray-300 bg-gray-50 rounded-lg shadow-sm text-lg focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500"
>
<option value="" disabled>Select to unit</option>
<option value="Celsius">Celsius</option>
<option value="Fahrenheit">Fahrenheit</option>
<option value="Kelvin">Kelvin</option>
</select>
<button
onClick={handleConvert}
className={`w-full px-8 py-3 bg-indigo-600 text-white font-semibold rounded-lg shadow-md hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-opacity-50 ${isConvertDisabled ? 'opacity-50 cursor-not-allowed' : ''}`}
disabled={isConvertDisabled}
>
Convert
</button>
<div className="mt-6 text-xl font-medium text-gray-700 text-center">
{convertedTemperature !== '' ? (
<p>Converted Temperature: <span className="text-indigo-600">{convertedTemperature}</span> {toUnit}</p>
) : (
<p>No temperature converted yet</p>
)}
</div>
</div>
);
- Renders the component layout with:
- An input field for temperature.
- Two dropdowns for selecting the source and target units.
- A button to trigger the conversion, disabled if inputs are not filled.
- A display area for showing the converted temperature or a message if no conversion has occurred yet.
export default TempConverter;
- Exports the
TempConverter
component for use in other parts of the application.
The App
component renders a TempConverter
component.
function App() {
return (
<div className="min-h-screen bg-gradient-to-r from-blue-500 to-indigo-600 flex items-center justify-center">
<div className="bg-white rounded-xl shadow-lg p-8 max-w-md mx-auto">
<h1 className="text-4xl font-bold text-center text-indigo-700 mb-8">Temperature Converter</h1>
<TempConverter />
</div>
</div>
);
}
- Import:
Provider
is imported fromreact-redux
. - Purpose: Wraps the entire application to provide the Redux store to all components via React’s context.
- Prop: The
store
prop ofProvider
is set tostoretemp
. - Value:
storetemp
is the configured Redux store, imported from./app/tempStore.js
. - Function: Allows components within the
Provider
to access the Redux store, enabling them to read from and dispatch actions to the store.
ReactDOM.createRoot(document.getElementById('root')).render(
<Provider store={storetemp}>
<App />
</Provider>,
)