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

151
Visualizações
How to detect radio button changed in React App

I have a react app where I have a table of radio buttons and I am looping through them

here is my code:

const setValue = (item, position) => {
    switch(item[0]) {
        case 'dashboard':
            if(permissions.dashboard == item[1]) {
                return true;
            }
            break;
        case 'user_management':
            if(permissions.user_management == item[1]) {
                return true;
            }
            break;
        case 'users_feedback':
            if(permissions.users_feedback == item[1]) {
                return true;
            }
            break;
        case 'settings':
            if(permissions.settings == item[1]) {
                return true;
            }
            break;
        case 'content_management':
            if(permissions.content_management == item[1]) {
                return true;
            }
            break;
        case 'influencers':
            if(permissions.influencers == item[1]) {
                return true;
            }
            break;
    }
}

const handleChangePermission = (e) => {
    console.log('Handle Change');
}

Object.entries(permissions).map((item, i) => (
     <tr className="permission-row" key={item[0]}>
         <td>{item[0]}</td>
         <td><input type="radio" id={item[0] + i} name={item[0]} defaultChecked={setValue(item, i)}  onChange={(e) => handleChangePermission(e.target.value)}/></td>
         <td><input type="radio" id={item[0] + i} name={item[0]} defaultChecked={setValue(item, i)} onChange={(e) => handleChangePermission(e.target.value)}/></td>
         <td><input type="radio" id={item[0] + i} name={item[0]} defaultChecked={setValue(item, i)}  onChange={(e) => handleChangePermission(e.target.value)}/></td>
     </tr>
))

and here what it looks like: enter image description here

and this is the permissions object I have:

{
"dashboard": 3,
"user_management": 3,
"users_feedback": 3,
"settings": 3,
"content_management": 3,
"influencers": 3
}

I have an object with default values called permissions and I am successfully looping through them and displaying the right data. My question is, how do I detect a radio button change, so I can change it in that object permissions

Edit: I added the onChange method but still it is not triggering what seems to bet the problem here ??

P.S I'm using reactstrap library radio buttons for styling

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

0

There were some fixes to made.

  1. Use onClick instead of onChange event to perform functions with radio buttons. Refer this answer for more.
  2. According to your requirements, the value of your radio button should be hardcoded as value=1, value=2, value=3 respectively.
  3. Modify your handleChangePermission as below.

JSX

  <td>
      <input
          type="radio" id={item[0] + i} 
          name={item[0]} defaultChecked={setValue(item, i)} 
          value="1" onClick={handleChangePermission} />
   </td>

Modified function

const handleChangePermission = (e) => {
    const name = e.target.name;
    const value = parseInt(e.target.value);
    permissions[name] = value;
  };

Here we simply define the handleChangePermission function to accept synthetic event so we can access the name and value of radio input button. Since the name and value of radio button are the same values for the key and value of permissions object we can assign access the key and modify the value as above

Here is the solution on sandbox

about 4 years ago · Juan Pablo Isaza Relatório

0

add the second argument to map(it work like iterator - from 0 to the last element of array - [0,1,2,3,4...] :

object.map((number, i) =>
  <li defaultChecked ={() => setValue(item, i)}></li>
);

to detect change of radio - add just onChange() with function:

const function = () => {
//your code
}

<CustomInput onChange={() => function} type="radio" id={item[0] + i} name={item[0]} defaultChecked={setValue(item, i)} />
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