728x90
리액트에서 아래의 코드는 값은 변경되지만 화면에서 어떤 값이 삭제되지 않는다. 이유는 가상 dom에서 리랜더링 되지 않기 때문이다.
const delOptPart = (option_idx:number, optPart_idx:string) => {
const selectedOpt = options.find(option => option.option_index === option_idx);
const newOptionParts = selectedOpt!.option_parts!.filter(optPart => optPart.optPart_idx !== optPart_idx);
delete selectedOpt?.option_parts;
selectedOpt!.option_parts = newOptionParts!;
console.log("selectedOpt", selectedOpt!);
//6.18 테스트 필요
}
해결 방법 중 useState를 넣고 삭제 카운트를 넣었다.
const [delOpPartCount, setDelOpPartCount] = useState(0);🌟
const delOptPart = (option_idx:number, optPart_idx:string) => {
//삭제 sensor
setDelOpPartCount((prev) => prev+1);🌟
const selectedOpt = options.find(option => option.option_index === option_idx);
const newOptionParts = selectedOpt!.option_parts!.filter(optPart => optPart.optPart_idx !== optPart_idx);
delete selectedOpt?.option_parts;
selectedOpt!.option_parts = newOptionParts!;
console.log("selectedOpt", selectedOpt!);
//6.18 테스트 필요
}
728x90
'Typescript & React' 카테고리의 다른 글
[React]contains an input of type email with both value and defaultValue props. Input elements must be either controlled or uncontrolled (0) | 2024.06.25 |
---|---|
'delete' 연산자의 피연산자는 선택 사항이어야 합니다.ts(2790) (0) | 2024.06.18 |
Typscript 문법 오류 (0) | 2024.06.14 |
React에서 react-hook-form 새로고침 문제 해결 방법은? (0) | 2024.06.12 |
SyntaxError: Unexpected end of JSON input (0) | 2024.02.15 |