Skip to content

Commit d890a0e

Browse files
committed
feat: add EquatableView section
1 parent 8892688 commit d890a0e

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

README.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,28 @@ With function builders, you can even create your own DSLs. And this year in WWDC
727727
Apple released more features based on function builders, like **WidgetKit** and
728728
SwiftUI **App Structure**.
729729

730+
How SwiftUI determine when to update a view?
731+
--------------------------------------------
732+
All views in SwiftUI are like **PureComponent** in React by default. That means,
733+
all the member variables (props) will be used to evaluate the equality, of course
734+
it's shallow comparison.
735+
736+
What if you want to customize the update strategy? If you take a look at the
737+
declaration of ``View`` protocol, you will notice this subtle thing:
738+
739+
.. code-block:: swift
740+
741+
extension View where Self : Equatable {
742+
743+
/// Prevents the view from updating its child view when its new value is the
744+
/// same as its old value.
745+
@inlinable public func equatable() -> EquatableView<Self>
746+
}
747+
748+
SwiftUI provides an ``EquatableView`` to let you achieve that. All you need to do
749+
is make your view type conform ``Equatable`` and implement the ``==`` function.
750+
Then wrap it into ``EquatableView`` at the call-site.
751+
730752
.. References:
731753
732754
.. _`Thinking in React Hooks`: https://wattenberger.com/blog/react-hooks

0 commit comments

Comments
 (0)