-
Notifications
You must be signed in to change notification settings - Fork 68
CPLAT-8035 Implement/Expose useEffect Hook #227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8bde18c
44255e9
d9f5163
9bd0bf3
ca578f5
30a81b1
6211f98
dd78b2c
eac37cd
745d07a
e3fe834
de5292e
87b912f
2435c30
4721778
d0af4d2
880c7d3
d28648b
88cddec
29b128f
d30390b
5c76190
716238b
368f13e
1503565
2798ad1
8668f9e
f8a7fe3
0c6b313
e5ed461
95142fd
55f8151
e234b38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,6 +102,62 @@ StateHook<T> useState<T>(T initialValue) => StateHook(initialValue); | |
/// Learn more: <https://reactjs.org/docs/hooks-reference.html#lazy-initial-state>. | ||
StateHook<T> useStateLazy<T>(T init()) => StateHook.lazy(init); | ||
|
||
/// Runs [sideEffect] after every completed render of a [DartFunctionComponent]. | ||
/// | ||
/// If [dependencies] are given, [sideEffect] will only run if one of the [dependencies] have changed. | ||
/// [sideEffect] may return a cleanup function that is run before the component unmounts or re-renders. | ||
/// | ||
/// > __Note:__ there are two [rules for using Hooks](https://reactjs.org/docs/hooks-rules.html): | ||
/// > | ||
/// > * Only call Hooks at the top level. | ||
/// > * Only call Hooks from inside a [DartFunctionComponent]. | ||
/// | ||
/// __Example__: | ||
/// | ||
/// ``` | ||
/// UseEffectTestComponent(Map props) { | ||
/// final count = useState(1); | ||
/// final evenOdd = useState('even'); | ||
/// | ||
/// useEffect(() { | ||
/// if (count.value % 2 == 0) { | ||
/// evenOdd.set('even'); | ||
/// } else { | ||
/// evenOdd.set('odd'); | ||
/// } | ||
/// return () { | ||
/// print('count is changing... do some cleanup if you need to'); | ||
/// }; | ||
/// | ||
/// // This dependency prevents the effect from running every time [evenOdd.value] changes. | ||
/// }, [count.value]); | ||
sydneyjodon-wk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// | ||
/// return react.div({}, [ | ||
/// react.p({}, ['${count.value} is ${evenOdd.value}']), | ||
/// react.button({'onClick': (_) => count.set(count.value + 1)}, ['+']), | ||
/// ]); | ||
/// } | ||
/// ``` | ||
/// | ||
/// See: <https://reactjs.org/docs/hooks-effect.html#tip-optimizing-performance-by-skipping-effects>. | ||
void useEffect(dynamic Function() sideEffect, [List<Object> dependencies]) { | ||
var wrappedSideEffect = allowInterop(() { | ||
var result = sideEffect(); | ||
if (result is Function) { | ||
return allowInterop(result); | ||
} | ||
|
||
/// When no cleanup function is returned, [sideEffect] returns undefined. | ||
return jsUndefined; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add a comment saying why undefined is needed? |
||
}); | ||
|
||
if (dependencies != null) { | ||
return React.useEffect(wrappedSideEffect, dependencies); | ||
Comment on lines
+154
to
+155
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it ok / reasonably efficient to pass in an empty list here? If not, we should probably check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, passing an empty list is used if you just want to run an effect and clean it up only once (on mount and unmount) |
||
} else { | ||
return React.useEffect(wrappedSideEffect); | ||
} | ||
} | ||
|
||
/// Returns a memoized version of [callback] that only changes if one of the [dependencies] has changed. | ||
/// | ||
/// > __Note:__ there are two [rules for using Hooks](https://reactjs.org/docs/hooks-rules.html): | ||
|
Uh oh!
There was an error while loading. Please reload this page.