본문 바로가기
카테고리 없음

[React js] how to make toggle by input type checkbox

by 고집프로그래머 2022. 7. 5.
반응형

Hello this is GOJIP programmer. 

 

I just started write for share information about React js.

 
Usually, we use

 </input type="checkbox" > 

</input type="checkbox" >

checkbox like this, but sometimes we use this with making toggle.


When I foung this with google, I only found

<input type="checkbox" defualtvalue={true} >

this, they told me to use "defualtvlaue"

But didn't work really becuse input type is checkbox.

  Here is how I made,

const [checked, setChecked] = React.useState(true);
<input type="checkbox" checked={checked} onClick={()=>setChecked(!checked) /> 

 

U have to use "checked" parameter.

 

If you want to put API value, then


<input type="checkbox" checked={API_VALUE} onClick={()=>setChecked(!API_VALUE) /> 


 
use like this.

 

Thank you

728x90
반응형