I am having trouble making a checkbox react to its onChange event(checkbox remains in its default state). I have seen similar post here, but nothing really seems to work. I'm hoping a different set of eyes could help figure out whats wrong with my logic.
Here is the handleChange function, and the initial values of the input fields which are both in the parent component
const [accountData, setAccountData]=useState({accountType:'Free', //default input value for text input
accountStatus: false }) //default input for checkbox input
const handleSubmit={/*submitLogic*/}
const handleChange = e =>{
const target = e.target;
const value = target.type === 'checkbox' ? target.checked : target.value;
const name = target.name;
setAccountData({...accountData, [name]:value})
}
//passing the handle change as a prop to the EditView component\\
<EditView handleChange={handleChange} handleSubmit={handleSubmit} data={accountData}/>
Here is the child component that contains the input form(Concern is the checkbox, as the text input field reacts to its onChange event).
const EditView = ({ handleChange, handleSubmit, data}) =>{
const isChecked=data.accountStatus;
<form onSubmit={handleSubmit}>
<div className="form-row" >
<div className="form-group col-md-6">
< label htmlFor="accountType">Account Type:</label>
<input
type="text"
className="form-control"
id="accountType"
name="accountType"
value={data.accountType}
onChange={handleChange}/>
</div>
<div className="form-group col-md-6">
< label htmlFor="accountStatus">Account Status:</label>
<div className="custom-control custom-switch">
<input
type="checkbox"
className="custom-control-input "
id="accountStatus"
name='accountStatus'
checked={isChecked}
//value={isChecked}
onChange={handleChange}
/>
</div>
</div>
<button type='submit' >Submit</button>
</form>
}
I even tried creating a different onChange handle function, but still had the same problem. Have I done something wrong in the above code that makes my checkbox not change from its default state?
< label htmlForwill not render properly