Skip to content

Commit

Permalink
Create 241028_confirmationDialog,_LazyVStack.md
Browse files Browse the repository at this point in the history
  • Loading branch information
leeari95 committed Oct 28, 2024
1 parent cbf6c7e commit ddae03a
Showing 1 changed file with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 241028 confirmationDialog, LazyVStack

LazyVStack ๋‚ด๋ถ€์— ์žˆ๋Š” ๋ทฐ๋“ค์ด confirmationDialog๋ฅผ ๋„์šฐ์ง€ ๋ชปํ•˜๋Š” ํ˜„์ƒ

10์›” 28์ผ (์›”)

# ํ•™์Šต๋‚ด์šฉ

- confirmationDialog์„ LazyVStack ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์˜ค๋™์ž‘ ๋ฌธ์ œ

# ๊ณ ๋ฏผํ•œ ์  / ํ•ด๊ฒฐ๋ฐฉ๋ฒ•

## ์›์ธ

> ์‚ฌ์‹ค ์ •ํ™•ํ•œ ์›์ธ์€ ์ฐพ์ง€ ๋ชปํ–ˆ๋‹ค...
* Lazyํ•œ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๋ทฐ์˜ ์ƒ๋ช…์ฃผ๊ธฐ์™€ ์ƒํƒœ ๋ณ€๊ฒฝ์„ ์ตœ์ ํ™”ํ•˜๊ธฐ ์œ„ํ•ด ์ง€์—ฐ ๋กœ๋”ฉ์„ ์ ์šฉํ•˜๋Š”๋ฐ, ๋ฌธ์ œ์™€ ๊ด€๋ จ์ด ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ์ถ”์ธก๋œ๋‹ค.
* Lazy ์ปจํ…Œ์ด๋„ˆ๋Š” ์Šคํฌ๋กคํ•  ๋•Œ ๋ทฐ๋ฅผ ํ•„์š”์— ๋”ฐ๋ผ ์ƒ์„ฑํ•˜๊ณ  ์ œ๊ฑฐํ•˜๋ฉด์„œ ์„ฑ๋Šฅ์„ ์ตœ์ ํ™”ํ•˜๋ฏ€๋กœ confirmationDialog์˜ isPresented ์ƒํƒœ ๋ณ€ํ™”๋ฅผ ์ฆ‰๊ฐ์ ์œผ๋กœ ๋ฐ˜์˜ํ•˜์ง€ ๋ชปํ•˜๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

์ •๋ฆฌํ•˜๋ฉด, Lazy ์ปจํ…Œ์ด๋„ˆ์˜ ์ตœ์ ํ™”๊ฐ€ ๋ทฐ์˜ ์ƒํƒœ ๋ณ€ํ™”๋ฅผ ์ฆ‰์‹œ ๋ฐ˜์˜ํ•˜์ง€ ์•Š๋„๋ก ์ตœ์ ํ™”๋œ ์„ค๊ณ„๋กœ ์ธํ•ด confirmationDialog์˜ isPresented ๋ฐ”์ธ๋”ฉ์ด ์ฆ‰๊ฐ ๋ฐ˜์˜๋˜์ง€ ์•Š๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

## ํ•ด๊ฒฐ

### 1. Store์— shouldShowConfirmationDialog ๋ผ๋Š” State ๊ฐ’์„ ๋งŒ๋“ค์–ด์„œ ๊ด€๋ฆฌํ•˜๊ธฐ

* Store์˜ Action์— ๋ฒ„ํŠผ ํƒญ ์ด๋ฒคํŠธ๋ฅผ ๋‘๊ณ  ํƒญ ์ด๋ฒคํŠธ์— ๋”ฐ๋ผ shouldShowConfirmationDialog์— ๊ฐ’์„ ์žฌํ• ๋‹น์‹œ์ผœ์ค€๋‹ค. (ํƒ€์ž…์€ UUID)
* ๊ทธ๋ฆฌ๊ณ  ๋ทฐ์—์„œ๋Š” onChange๋ฅผ ํ†ตํ•ด store.state.shouldShowConfirmationDialog๋ฅผ ์ถ”์ ํ•˜๋ฉด์„œ ๊ฐ’์ด ์žฌํ• ๋‹น ๋  ๋•Œ๋งˆ๋‹ค ์‹ค์ œ๋กœ confirmationDialog๋ฅผ ๋„์šธ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์ƒํƒœ๊ฐ’์„ toggleํ•ด์ฃผ๋„๋ก ํ•œ๋‹ค.

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ฒ„ํŠผ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ store์— ๊ด€๋ จ ์•ก์…˜์„ ๋ณด๋‚ด๊ฒŒ ๋˜๊ณ , shouldShowConfirmationDialog ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋ฉด์„œ ๋ทฐ์—์„œ ์ด๋ฅผ ๊ฐ์ง€ํ•˜๊ณ  ๊ด€๋ จ๋œ ์ƒํƒœ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ฒŒ ๋˜๋ฉด์„œ confirmationDialog๋ฅผ ๋„์šฐ๋Š” ํ˜•ํƒœ๊ฐ€ ๋œ๋‹ค.

### 2. onChange ์ˆ˜์ •์ž ํ™œ์šฉํ•˜๊ธฐ

* ์ด ๋ฐฉ๋ฒ•์€ ๋‹จ์ˆœํžˆ onChange ์ˆ˜์ •์ž๋ฅผ ํ†ตํ•ด confirmationDialog ์ˆ˜์ •์ž๊ฐ€ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ์ƒํƒœ๊ฐ’์„ ์ถ”์ ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.
* ํ•ด๊ฒฐ๋˜๋Š” ์ด์œ ๋ฅผ ์ถ”์ธกํ•ด๋ณด์ž๋ฉด, onChange๋ฅผ ํ†ตํ•ด ์ƒํƒœ๊ฐ’ ๋ณ€ํ™”๋ฅผ ๊ฐ•์ œ๋กœ ๊ฐ์ง€ํ•˜๊ฒŒ ๋˜๋ฉด์„œ ๋ทฐ ํŠธ๋ฆฌ๊ฐ€ ํ•ด๋‹น ๋ณ€ํ™”๋ฅผ ์ฆ‰์‹œ ๋ฐ˜์˜ํ•˜๋„๋ก ์œ ๋„ํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.
* ๋”ฐ๋ผ์„œ Lazy ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ง€์—ฐ ๋กœ๋”ฉ์œผ๋กœ ์ธํ•ด ์ƒํƒœ ๋ณ€ํ™”๋ฅผ ์ธ์‹ํ•˜์ง€ ๋ชปํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ๋ณด์™„ํ•ด์ค€๋‹ค.

```swift
.onChange(of: showActionSheet) { _ in
// Workaround: Lazy ์ปจํ…Œ์ด๋„ˆ ๋‚ด์—์„œ lazy ๋กœ๋“œ๋œ View๊ฐ€ confirmationDialog์˜ isPresented ๋ฐ”์ธ๋”ฉ ๊ฐ’์„ ์ œ๋Œ€๋กœ ๊ฐ์ง€ํ•˜์ง€ ๋ชปํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค.
// ์ด ๊ฒฝ์šฐ, onChange๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐ”์ธ๋”ฉ๋œ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ์ง€ํ•˜๋„๋ก ์ถ”๊ฐ€ํ•˜๋ฉด ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘ํ•จ.
}
.confirmationDialog(item.shorts.title, isPresented: $showActionSheet, titleVisibility: .visible) {
```


---

# ์ฐธ๊ณ  ๋งํฌ

- [https://stackoverflow.com/questions/74377118/swiftui-confirmationdialog-has-abnormal-behavior-when-inside-a-lazyvstack](https://stackoverflow.com/questions/74377118/swiftui-confirmationdialog-has-abnormal-behavior-when-inside-a-lazyvstack)

0 comments on commit ddae03a

Please sign in to comment.