The partial code is from the CurrencyFilter.js feature (the full code for this component is at bottom of this post).
I understand the following code which displays the three currency buttons on the web page with the exception of this line
className={`currency-button ${ currencyFilter === currency && 'selected' }`}
Code and relevant CSS:
return (
<div id="currency-filters-container">
<h3>Choose a currency</h3>
{currenciesData.map(createCurrencyButton)} {/*for each currency in ['USD', 'EUR', 'CAD'] createCurrrencyButton will be called */}
</div>
);
/*from currenciesData from data.js, the function below will be called 3 times hence three buttons will be created */
function createCurrencyButton(currency) {
return (
<button
**className={`currency-button ${ currencyFilter === currency && 'selected' }`}**
key={currency}
onClick={() => onClickHandler(currency)}
>
{currency} {/* label for buttonL USD, EUR, CAN */}
</button>
);
}
CSS:
.currency-button {
background: white;
border: 1px solid var(--black);
padding: 8px;
font-weight: 600;
cursor: pointer;
}
.currency-button:first-of-type {
border-top-left-radius: var(--border-radius);
border-bottom-left-radius: var(--border-radius);
}
.currency-button:last-of-type {
border-top-right-radius: var(--border-radius);
border-bottom-right-radius: var(--border-radius);
}
.currency-button.selected {
background: var(--black);
color: white;
}
Complete CurrencyFilter.js
import React from 'react';
import { currenciesData } from '../../data.js';
import { setCurrency } from './currencyFilterSlice.js';
//The currencyFilter component rendered at top of page
//currencyFilter and dispatch passed in from App.js
//this will render 'Choose a currency and three buttons "USD, EUR, CAD"
export const CurrencyFilter = ({ currencyFilter, dispatch }) => {
console.log(currencyFilter)
const onClickHandler = (currency) => {
dispatch(setCurrency(currency)); //imported from the currency slice (see above)
};
return (
<div id="currency-filters-container">
<h3>Choose a currency</h3>
{currenciesData.map(createCurrencyButton)} {/*for each currency in ['USD', 'EUR', 'CAD'] createCurrrencyButton will be called */}
</div>
);
/*from currenciesData from data.js, the function below will be called 3 times hence three buttons will be created */
function createCurrencyButton(currency) {
return (
<button
className={`currency-button ${
currencyFilter === currency && 'selected'
}`}
key={currency}
onClick={() => onClickHandler(currency)}
>
{currency} {/* label for buttonL USD, EUR, CAN */}
</button>
);
}
};