Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!