fatbobman / blogComments

1 stars 0 forks source link

理解 SwiftUI 的视图刷新机制 #256

Open fatbobman opened 2 weeks ago

fatbobman commented 2 weeks ago

理解 SwiftUI 的视图刷新机制:从 TimelineView 刷新问题谈起

在 SwiftUI 中,视图的自动刷新机制让我们能够轻松构建响应式的用户界面。但有时,视图可能并不会按照我们的预期进行更新。本文将通过一个看似简单但颇具代表性的 TimelineView 刷新问题,探讨 SwiftUI 的视图刷新机制。

Understanding SwiftUI's View Update Mechanism: Starting from a TimelineView Update Issue

In SwiftUI, the automatic view update mechanism allows us to easily build responsive user interfaces. However, sometimes views may not update as we expect. This article explores SwiftUI's view update mechanism through a seemingly simple but representative TimelineView update issue.

agelessman commented 6 days ago

强强强

qiukun commented 6 days ago

没从 react 学起导致的

pappalar commented 3 days ago

I am not sure I understand the solution of the article. The issue with RightEmoji is that the emoji is recalculated inside the body of the view, but the body of the RightEmoji is never recalculated because the new instance of RightEmoji has no change from the one in the view hiearchy

The "correct" solution would be to calculate the randomEmoji in the init and assign it to a @State variable.

As you explained in the article, views body is recalculated if the View changes. So the example of using a random let id is confusing.

IMHO the good practice here would be to follow the SwiftUI pattern and use Observed property correctly.