Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

295
Visualizações
How to change button style on click in react bootstrap

I have a a toggle button to which I want to add my own styling, I want the button to change color when it has been selected, but the problem with current code is that when the button is being pressed the color changes to desired result but as soon as I release my click, the button color goes back to its default styling. Can someone please help me with this

My React Component

import React, { useState } from 'react';
import ToggleButton from 'react-bootstrap/ToggleButton';
import ToggleButtonGroup from 'react-bootstrap/ToggleButtonGroup';
import '../styles/components/_LoanType.scss';

const LoanType:React.VFC = () => {
    const [selectedButton, setSelectedButtonValue] = useState<string>('fixedRateLoan');
    const handleChange = (value:string) => {
        selectedButton(value);
    }

    return (
        <div className="loan-type">
            <ToggleButtonGroup type="radio" name="radio" value={selectedButton} onChange={handleChange} >
                <ToggleButton value={'frl'}>1</ToggleButton>
                <ToggleButton value={'vrl'}>2</ToggleButton>
                <ToggleButton value={'spl'}>3</ToggleButton>
            </ToggleButtonGroup>
        </div>
    );
}

export default LoanType;

My scss file:

.pkg .calc {
    .ci .lts{
        .loan-type {
            .btn-group{
         

   display: flex;
            .btn-primary{
                text-align: center;
                background-color: #ffffff;
                color: #656565;
                font-weight: 900;
                &:active{
                    color: #ffffff;
                    background-color: #656565;
                }
            }        
        }
    }
}

}

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

First of all, there are some errors in your component.

In the change handler, you're calling the current state as a function, instead of the function that updates the state.

const handleChange = (value:string) => {
    selectedButton(value);
}

Should be

const handleChange = (value:string) => {
    setSelectedButtonValue(value);
}

Secondly, the documentation for <ToggleButton> states that an id is required for button clicks to toggle input. So you should add this to the buttons, e.g.

<ToggleButton id="frl" value={"frl"}>
  1
</ToggleButton>
<ToggleButton id="vrl" value={"vrl"}>
  2
</ToggleButton>
<ToggleButton id="spl" value={"spl"}>
  3
</ToggleButton>

You're also initializing the state with a value that is not an option. Perhaps this should be set to either frl, vrl or spl?

useState<string>('frl');

Hope this helps!

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda