Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

125
Vistas
Get all the fields' values that display on screen in ReactJs

In my ReactJs app I want to get only the values of the fields that shows to the screen, for example I have some radio groups and one group will be hide when I choose specific value like the code below:

const App = () => {
  const [value, setValue] = useState();
  const [value2, setValue2] = useState();

  const onChange = (e) => {
    setValue(e.target.value);
  };

  const onChange2 = (e) => {
    setValue2(e.target.value);
  };

  return (
    <div className='container'>
      <div>
        <Radio.Group value={value} onChange={onChange}>
          <Radio value={1}>yes</Radio>
          <Radio value={2}>no</Radio>
        </Radio.Group>
      </div>

      {value === 1 ? null : (
        <div>
          <Radio.Group value={value2} onChange={onChange2}>
            <Radio value={3}>yes</Radio>
            <Radio value={4}>no</Radio>
          </Radio.Group>
        </div>
      )}
    </div>
  );
};

as you can see in my simple code, when the value at the first radio groud is '1' the second radio group will be hide. but, if I choose in the second radio group some value (3 or 4) and then select in the first radio group the value '1', the value in the second still will be what I choose before.

This case is simple to reset one field or ignore it, but if I have a form with 20 fields, I want to get only the fields that shows on screen, even if I did not reset the hide fields.

There is a way to get all the values that render in 'container' div or better way to do it?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Try this

// This means "whenever the second group becomes hidden, reset its state to default"
useEffect(() => {
  const secondIsHidden = value === 1;
  if (secondIsHidden) setValue2(undefined); // Or whatever
}, [value]);
about 4 years ago · Juan Pablo Isaza Denunciar

0

I created a state and with each key, i set the boolean value. If its not-hidden, i set false otherwise true.


const App = () => {
    const [value, setValue] = useState();
    const [value2, setValue2] = useState();

    const [radioValues, setRadioValues] = useState({ 1: false, 2: false, 3: false, 4: false });

    const onChange = (e: any) => {
        setRadioValues((prev) => {
            let newValues: any = {};
            Object.entries(prev).forEach((item: any) => {
                newValues[parseInt(item[0])] = e.target.value === 1 ? [3, 4].includes(parseInt(item[0])) : false;
            });
            return newValues;
        });
        setValue(e.target.value);
    };

    const onChange2 = (e: any) => {
        setValue2(e.target.value);
    };

    console.log(Object.keys(radioValues).filter((key) => !radioValues[key]));

    return (
        <div className='container'>
            <div>
                <Radio.Group value={value} onChange={onChange}>
                    <Radio value={1}>yes</Radio>
                    <Radio value={2}>no</Radio>
                </Radio.Group>
            </div>

            {value === 1 ? null : (
                <div>
                    <Radio.Group value={value2} onChange={onChange2}>
                        <Radio value={3}>yes</Radio>
                        <Radio value={4}>no</Radio>
                    </Radio.Group>
                </div>
            )}
        </div>
    );
};

export default App;

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda