Skip to content

Commit 5bfdb8f

Browse files
Merge pull request #372 from Workiva/prepare-for-7_0_0
FEDX-414 Add APIs and update deprecations in preparation for 7.0.0 release
2 parents e439306 + a4a30a6 commit 5bfdb8f

12 files changed

+299
-173
lines changed

CHANGELOG.md

+42
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,45 @@
1+
## 7.0.0-wip
2+
3+
- Migrate to null safety
4+
5+
#### Deprecated API removals
6+
- forwardRef (use forwardRef2 instead)
7+
- memo (use memo2 instead)
8+
- main (use htmlMain instead)
9+
- Ref class constructors: default and `useRefInit` (use useRef/createRef instead)
10+
- ReducerHook and StateHook class constructors (use hook functions instead).
11+
- APIs that have been no-ops since react-dart 6.0.0
12+
- SyntheticEvent members `persist` and `isPersistent`
13+
- unconvertJsEventHandler
14+
- APIs that were never intended for public use:
15+
- JsPropValidator
16+
- dartInteropStatics
17+
- ComponentStatics(2)
18+
- createReactDartComponentClass(2)
19+
- JsComponentConfig(2)
20+
- ReactDartInteropStatics
21+
- InteropContextValue
22+
- markChildrenValidated
23+
24+
#### Other API breakages
25+
- ReducerHook and StateHook have no public constructors and can no longer be extended
26+
- Ref.fromJs is now a factory constructor, meaning the Ref class can no longer be extended
27+
- ReactComponentFactoryProxy.call and .build return type changed from dynamic to ReactElement
28+
- This matches the type returned from `build` for all subclasses, which is what’s returned by call, and reflects the type returned at runtime
29+
- Has potential to cause some static analysis issues, but for the most part should not affect anything since ReactElement is typically treated as an opaque type
30+
- Needs consumer tests
31+
- Top-level component factories are typed as ReactDomComponentFactoryProxy instead of being `dynamic`: react.div
32+
- All PropValidatorInfo arguments are required
33+
- Changes to public but internal code that should not affect consumers:
34+
- ReactDartComponentInternal
35+
- Constructor now takes a required argument, props is final
36+
- initComponentInternal arguments are typed to reflect runtime assumptions
37+
- ReactComponentFactoryProxy no longer `implements Function`
38+
- This should not be a breakage, since as of Dart 2.0 inheriting from Function has had no effect
39+
40+
#### Potential behavior breakages
41+
- Component and Component2 members `props`/`state`/`jsThis` are late, will now throw instead of being null if accessed before initialized (e.g., in a constructor, final class field, or static lifecycle method).
42+
143
## [6.2.1](https://github.com/Workiva/react-dart/compare/6.2.0...6.2.1)
244
- [#366] Fix lints and eliminate most implicit casts
345

lib/hooks.dart

+44-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class StateHook<T> {
2323
/// The second item in the pair returned by [React.useState].
2424
void Function(dynamic) _setValue;
2525

26+
@Deprecated('Use useState instead. Will be removed in 7.0.0.')
2627
StateHook(T initialValue) {
2728
final result = React.useState(initialValue);
2829
_value = result[0] as T;
@@ -33,6 +34,7 @@ class StateHook<T> {
3334
/// initialize [_value] to the return value of [init].
3435
///
3536
/// See: <https://reactjs.org/docs/hooks-reference.html#lazy-initial-state>.
37+
@Deprecated('Use useStateLazy instead. Will be removed in 7.0.0.')
3638
StateHook.lazy(T Function() init) {
3739
final result = React.useState(allowInterop(init));
3840
_value = result[0] as T;
@@ -171,6 +173,7 @@ class ReducerHook<TState, TAction, TInit> {
171173
/// The second item in the pair returned by [React.useReducer].
172174
void Function(TAction) _dispatch;
173175

176+
@Deprecated('Use useReducer instead. Will be removed in 7.0.0.')
174177
ReducerHook(TState Function(TState state, TAction action) reducer, TState initialState) {
175178
final result = React.useReducer(allowInterop(reducer), initialState);
176179
_state = result[0] as TState;
@@ -181,6 +184,7 @@ class ReducerHook<TState, TAction, TInit> {
181184
/// initialize [_state] to the return value of [init(initialArg)].
182185
///
183186
/// See: <https://reactjs.org/docs/hooks-reference.html#lazy-initialization>.
187+
@Deprecated('Use useReducerLazy instead. Will be removed in 7.0.0.')
184188
ReducerHook.lazy(
185189
TState Function(TState state, TAction action) reducer, TInit initialArg, TState Function(TInit) init) {
186190
final result = React.useReducer(allowInterop(reducer), initialArg, allowInterop(init));
@@ -392,7 +396,46 @@ T useContext<T>(Context<T> context) => ContextHelpers.unjsifyNewContext(React.us
392396
/// ```
393397
///
394398
/// Learn more: <https://reactjs.org/docs/hooks-reference.html#useref>.
395-
Ref<T> useRef<T>([T initialValue]) => Ref.useRefInit(initialValue);
399+
Ref<T> useRef<T>([
400+
// This will eventually be deprecated, but not just yet.
401+
// @Deprecated('Use `useRefInit` instead to create refs with initial values.'
402+
// ' Since the argument to useRefInit is required, it can be used to create a Ref that holds a non-nullable type,'
403+
// ' whereas this function can only create Refs with nullable type arguments.')
404+
T initialValue,
405+
]) =>
406+
useRefInit(initialValue);
407+
408+
/// Returns a mutable [Ref] object with [Ref.current] property initialized to [initialValue].
409+
///
410+
/// Changes to the [Ref.current] property do not cause the containing [DartFunctionComponent] to re-render.
411+
///
412+
/// The returned [Ref] object will persist for the full lifetime of the [DartFunctionComponent].
413+
/// Compare to [createRef] which returns a new [Ref] object on each render.
414+
///
415+
/// > __Note:__ there are two [rules for using Hooks](https://reactjs.org/docs/hooks-rules.html):
416+
/// >
417+
/// > * Only call Hooks at the top level.
418+
/// > * Only call Hooks from inside a [DartFunctionComponent].
419+
///
420+
/// __Example__:
421+
///
422+
/// ```dart
423+
/// UseRefTestComponent(Map props) {
424+
/// final countRef = useRefInit(0);
425+
///
426+
/// handleClick([_]) {
427+
/// ref.current = ref.current + 1;
428+
/// window.alert('You clicked ${ref.current} times!');
429+
/// }
430+
///
431+
/// return react.Fragment({}, [
432+
/// react.button({'onClick': handleClick}, ['Click me!']),
433+
/// ]);
434+
/// }
435+
/// ```
436+
///
437+
/// Learn more: <https://reactjs.org/docs/hooks-reference.html#useref>.
438+
Ref<T> useRefInit<T>(T initialValue) => Ref.useRefInit(initialValue);
396439

397440
/// Returns a memoized version of the return value of [createFunction].
398441
///

0 commit comments

Comments
 (0)