Skip to content

Commit

Permalink
Deprecate calculateChangedBits and update tests for React 18
Browse files Browse the repository at this point in the history
  • Loading branch information
greglittlefield-wf committed Oct 18, 2024
1 parent 74aa3dd commit 7c4bed2
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
10 changes: 1 addition & 9 deletions example/test/function_component_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,7 @@ UseContextTestComponent(Map props) {
]);
}

int calculateChangedBits(currentValue, nextValue) {
var result = 1 << 1;
if (nextValue['renderCount'] % 2 == 0) {
result |= 1 << 2;
}
return result;
}

var TestNewContext = react.createContext<Map>({'renderCount': 0}, calculateChangedBits);
var TestNewContext = react.createContext<Map>({'renderCount': 0});

var newContextProviderComponent = react.registerComponent2(() => _NewContextProviderComponent());

Expand Down
1 change: 1 addition & 0 deletions lib/src/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class Context<T> {
/// Learn more: https://reactjs.org/docs/context.html#reactcreatecontext
Context<TValue> createContext<TValue>([
TValue? defaultValue,
@Deprecated('This has no effect in React 18, and there is no replacement for it.')
int Function(TValue? currentValue, TValue? nextValue)? calculateChangedBits,
]) {
final jsDefaultValue = ContextHelpers.jsifyNewContext(defaultValue);
Expand Down
9 changes: 5 additions & 4 deletions test/react_context_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ main() {
sharedTypeTests(testTypeValue);
});

group('calculateChangeBits argument functions correctly', () {
group('calculateChangeBits argument does not throw when used (has no effect in React 18)', () {
_ContextProviderWrapper? providerRef;
_ContextConsumerWrapper? consumerEvenRef;
_ContextConsumerWrapper? consumerOddRef;
Expand Down Expand Up @@ -77,15 +77,14 @@ main() {
});

test('on value updates', () {
// Test common behavior between React 17 (calculateChangedBits working)
// and React 18 (it having no effect).
providerRef!.increment();
expect(consumerEvenRef!.latestValue, 2);
expect(consumerOddRef!.latestValue, 1);
providerRef!.increment();
expect(consumerEvenRef!.latestValue, 2);
expect(consumerOddRef!.latestValue, 3);
providerRef!.increment();
expect(consumerEvenRef!.latestValue, 4);
expect(consumerOddRef!.latestValue, 3);
});
});
});
Expand All @@ -104,6 +103,8 @@ int calculateChangedBits(currentValue, nextValue) {
return result;
}


// ignore: deprecated_member_use_from_same_package
var TestCalculateChangedBitsContext = react.createContext(1, calculateChangedBits);

var TestContext = react.createContext();
Expand Down

0 comments on commit 7c4bed2

Please sign in to comment.