Answer the question
In order to leave comments, you need to log in
How to correctly assign a class to an active button in React/Redux?
There is a smart component and stupid. Buttons are rendered in stupid:
import React from 'react';
import Card from '@material-ui/core/Card';
import CardActions from '@material-ui/core/CardActions';
import CardContent from '@material-ui/core/CardContent';
import Button from '@material-ui/core/Button';
import Typography from '@material-ui/core/Typography';
import style from './method.module.scss'
import {useDispatch} from "react-redux";
import {getMethod} from "../../redux/action";
export default function Method({title, description, name}) {
const dispatch = useDispatch()
return (
<Card className={style.method}>
<CardActions>
<Button
name={name}
className={style.button}
size="small"
onClick={(event) => dispatch(getMethod(event.currentTarget.name))}
>
<CardContent>
<Typography variant="h5" component="h2">
{title}
</Typography>
<Typography color="textSecondary">
{description}
</Typography>
</CardContent>
</Button>
</CardActions>
</Card>
);
}
export function getMethod(method) {
return {
type: REQUES_METHOD,
payload: method
}
}
Answer the question
In order to leave comments, you need to log in
A smart component should read the property from the store where the name of the button is written and pass a boolean value to the stupid Method component, which, depending on this stupid value, adds the active class to the button or not.
export default function Method({title, description, name, isActive}) {
...
<Button
name={name}
className={style.button + (isActive ? ' active-class' : '')}
export default function Method({title, description, name, activeName}) {
const isActive = name === ActiveName
...
<Button
name={name}
className={style.button + (isActive ? ' active-class' : '')}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question