-3

I have went through this link "How to get only one value by selecting and switching to any of the option using reactjs?". I have similar component where i want to store either radio button value or Checkbox values in one variable which is in the App component. But i didn't find any solution to it. Can anyone help me in this? So that it would be useful for me and for the person who posted.

Chandan
  • 25
  • 8
  • 3
    Hi, welcome to stackoverflow. May I suggest you review [how to ask](https://stackoverflow.com/help/how-to-ask)? Please share your attempt and specifically what the issue is. – Drew Reese Mar 13 '20 at 05:39
  • @DrewReese - Hi, I have 2 components - one is child component - which have radio buttons and checkbox buttons, if we click on radio button or checkbox button then the value needs to get stored in App component variable (Parent component). – Chandan Mar 13 '20 at 05:47
  • It's generally acceptable to provide a [Minimal, Complete, and Reproducible](https://stackoverflow.com/help/minimal-reproducible-example) example of your code, without it help is not likely to arrive. What you describe seems to be a fairly simple scenario. What have you tried? – Drew Reese Mar 13 '20 at 05:53
  • @DrewReese - I apologize, But i've provided the link above in my query, where the similar question have been asked. So inorder to not to make any duplication of the query. I did not put similar coding – Chandan Mar 13 '20 at 06:20

1 Answers1

0

In App component:

const [value, setValue] = useState({
    radioValue: '',
    checkBoxValue: '',
});
const handleChangeInput = e => {
    setValue({
        ...value,
        [e.target.name]: e.target.value,
    });
};

In Child component:

<input
     type="radio"
     name="radioValue"
     value={value}
     onChange={e => handleInputChange(e)}
/>
iamhuynq
  • 5,357
  • 1
  • 13
  • 36