const major = ['회화', '조소', '판화', '금속공예', '도예', '유리공예', '목공예', '섬유공예', '기타']
<div>
<h2>카테고리*</h2>
<div>
{major.map(major =>
<label key={major} htmlFor='major'>
<input type='checkbox' id='major' />
{major}
</label>
)}
</div>
</div>
const [majorCheckedList, setMajorCheckedList] = useState<string[]>([]);
const [isChecked, setIsChecked] = useState(false);
const handleCheckedMajor = (value: string, isChecked: boolean) => {
if (isChecked) {
setMajorCheckedList((prev) => [...prev, value]);
return;
}
if (!isChecked && majorCheckedList.includes(value)) {
setMajorCheckedList(majorCheckedList.filter((item) => item !== value));
return;
}
return;
};
const handleOnChangeCheckMajor =
(e: ChangeEvent<HTMLInputElement>, value: string) => {
setIsChecked(!isChecked);
handleCheckedMajor(value, e.target.checked);
}
import React, { ChangeEvent, useState } from 'react'
<div>
<h2>카테고리*</h2>
<div>
{major.map(major =>
<label key={major} htmlFor='major'>
<input type='checkbox' id='major'
checked={majorCheckedList.includes(major)}
onChange={(e) => handleOnChangeCheckMajor(e, major)} />
{major}
</label>
)}
</div>
</div>
const handleOnSubmit = useCallback((e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
},
[majorCheckedList]
);
...
<form onSubmit={handleOnSubmit}>
<div>
<h2>카테고리*</h2>
<div>
{major.map(major =>
<label key={major} htmlFor='major'>
<input type='checkbox' id='major'
checked={majorCheckedList.includes(major)}
onChange={(e) => handleOnChangeCheckMajor(e, major)} />
{major}
</label>
)}
</div>
</div>
</form>
import React, { ChangeEvent, useCallback, useState } from 'react'
<div>
<p>
불순한 의도는 처벌을 피할 수 없습니다.(x10) </p>
<label htmlFor='agreement'><input type='checkbox' id='agreement' />
동의합니다.
</label>
</div>
const [agreementCheckedList, setAgreementCheckedList] = useState(false);
or
const [agreementCheckedList, setAgreementCheckedList] = useState<boolean>(false);
<div>
<p>
불순한 의도는 처벌을 피할 수 없습니다.(x10) </p>
<label htmlFor='agreement'>
<input type='checkbox' id='agreement'
checked={agreementCheckedList}
onChange={() => setAgreementCheckedList(!agreementCheckedList)} />
동의합니다.</label>
</div>
(1) https://junheedot.tistory.com/entry/%EB%A6%AC%EC%95%A1%ED%8A%B8%EC%97%90%EC%84%9C-%EC%B2%B4%ED%81%AC-%EB%B0%95%EC%8A%A4-%EB%8B%A4%EB%A3%A8%EA%B8%B0
(2) https://www.delftstack.com/ko/howto/react/react-checkbox-value/