You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this section, we will learn how to make the `Clock` component truly reusable and encapsulated. It will set up its own timer and update itself every second.
19
19
20
20
We can start by encapsulating how the clock looks:
However, it misses a crucial requirement: the fact that the `Clock` sets up a timer and updates the UI every second should be an implementation detail of the `Clock`.
25
25
@@ -52,7 +52,7 @@ You can convert a functional component like `Clock` to a class in five steps:
52
52
53
53
5. Delete the remaining empty function declaration.
@@ -321,15 +321,15 @@ This also works for user-defined components:
321
321
322
322
The `FormattedDate` component would receive the `date` in its props and wouldn't know whether it came from the `Clock`'s state, from the `Clock`'s props, or was typed by hand:
This is commonly called a "top-down" or "unidirectional" data flow. Any state is always owned by some specific component, and any data or UI derived from that state can only affect components "below" them in the tree.
327
327
328
328
If you imagine a component tree as a waterfall of props, each component's state is like an additional water source that joins it at an arbitrary point but also flows down.
329
329
330
330
To show that all components are truly isolated, we can create an `App` component that renders three `<Clock>`s:
0 commit comments