1
    <input className="form-control"
                        type="text"
                                               
                      />
<input type="button" onClick={()=>clickHandle()}/>

When I click the button, I want to change the check property of the checkbox. But the checked property or defaultChecked properties do not work in this regard. What can I do about it?

Fatih Baytar
  • 17
  • 1
  • 5
  • 2
    Please add some more context to your question (the basic structure of the component). Also, there is no checkbox in the code. – atomrc Mar 24 '21 at 09:22

2 Answers2

3

As far as I understand, you want the status of the checkbox to change when the button is clicked.

If I understood correctly, this is probably what you wanted.

export default function App() {
  const [chkValue, setChkValue] = useState(false);
  return (
    <div className="App">
      <input className="form-control" type="checkbox" checked={chkValue}/>
       <input type="button" value="click" onClick={()=>setChkValue(!chkValue)} />
    </div>
  );
}
-1

You have a type text input and a type button input. There is no checkbox there. You'd need a type checkbox input and then try this answer How to set default Checked in checkbox ReactJS?

Edit:

Wrong link for setting checked value. The link explains how to set the default. This code snippet has an example of setting a checkbox value dynamically.

pks9906
  • 193
  • 8
  • Isn't defaultChecked properties, initialize value? – Fatih Baytar Mar 24 '21 at 09:57
  • @FatihBaytar you are right sorry. checked is the correct property. https://reactjs.org/docs/forms.html#handling-multiple-inputs this link has an example in the code with a checkbox and how to set it correctly. – pks9906 Mar 24 '21 at 10:11
  • Thanks. I think my main problem is with dynamically generated checkboxes. – Fatih Baytar Mar 24 '21 at 10:23