Design comparison
SolutionDesign
Solution retrospective
Hi all! I have a problem in this task. Please tell me how to make the button change color when clicked, and the previous one returned to its original state. I would be grateful for any comments and additions
Community feedback
- @dvenomb98Posted about 2 years ago
To your question:
Set state that you can pass to components.
For example:
const [selected, setSelected] = useState("day")
then you change a style of a button based on selected state.
I use tailwindCSS for simpler work but u can do it via style attribute.
const buttons = [{value: "day", label: "Daily"}, {value: "week", label: "Weekly"}, {value: "month", label: "Monthly"}, ] <div> {buttons.map(({value, label}) => ( <button className={`${selected === value ? "bg-red" : "bg-neutral"} text-3xl`} onClick={() => setSelected(value)}> {label} </button> )) </div>
Marked as helpful0
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord