-
Notifications
You must be signed in to change notification settings - Fork 58
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
CPLAT-9347 Add memo for function components #613
Merged
rmconsole6-wk
merged 4 commits into
function-components-wip
from
cplat-9347-add-memo-hoc
Jul 30, 2020
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2020 Workiva Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
library over_react.memo; | ||
|
||
import 'package:over_react/src/component_declaration/component_type_checking.dart'; | ||
import 'package:react/react_client/react_interop.dart' as react_interop; | ||
import 'package:react/react_client.dart'; | ||
import 'package:over_react/component_base.dart'; | ||
|
||
/// A [higher order component](https://reactjs.org/docs/higher-order-components.html) for function components | ||
/// that behaves similar to the way [`React.PureComponent`](https://reactjs.org/docs/react-api.html#reactpurecomponent) | ||
/// does for class-based components. | ||
/// | ||
/// If your function component renders the same result given the same props, you can wrap it in a call to | ||
/// `memo` for a performance boost in some cases by memoizing the result. This means that React will skip | ||
/// rendering the component, and reuse the last rendered result. | ||
/// | ||
/// > __NOTE:__ This should only be used to wrap function components. | ||
/// | ||
/// ```dart | ||
/// import 'package:over_react/over_react.dart' as react; | ||
/// | ||
/// UiFactory<UiProps> MemoExample = memo<UiProps>(uiFunction( | ||
/// (props) { | ||
/// // render using props | ||
/// }, | ||
/// FunctionComponentConfig(), | ||
/// )); | ||
/// ``` | ||
/// | ||
/// `memo` only affects props changes. If your function component wrapped in `memo` has a | ||
/// [useState] or [useContext] Hook in its implementation, it will still rerender when `state` or `context` change. | ||
/// | ||
/// By default it will only shallowly compare complex objects in the props map. | ||
/// If you want control over the comparison, you can also provide a custom comparison | ||
/// function to the [areEqual] argument as shown in the example below. | ||
/// | ||
/// ```dart | ||
/// import 'package:over_react/over_react.dart' as react; | ||
/// | ||
/// UiFactory<MemoWithComparisonProps> MemoWithComparison = memo<MemoWithComparisonProps>(uiFunction( | ||
/// (props) { | ||
/// // render using props | ||
/// }, | ||
/// $MemoWithComparisonConfig, // ignore: undefined_identifier | ||
/// ), areEqual: (prevProps, nextProps) { | ||
/// // Do some custom comparison logic to return a bool based on prevProps / nextProps | ||
/// }); | ||
/// ``` | ||
/// | ||
/// > __This method only exists as a performance optimization.__ | ||
/// > | ||
/// > Do not rely on it to “prevent” a render, as this can lead to bugs. | ||
/// | ||
/// See: <https://reactjs.org/docs/react-api.html#reactmemo>. | ||
UiFactory<TProps> memo<TProps extends UiProps>(UiFactory<TProps> factory, | ||
{bool Function(TProps prevProps, TProps nextProps) areEqual, | ||
String displayName}) { | ||
enforceFunctionComponent(factory().componentFactory); | ||
|
||
ReactComponentFactoryProxy hoc; | ||
if (areEqual != null) { | ||
bool wrapProps(Map prevProps, Map nextProps) { | ||
final tPrevProps = factory(prevProps); | ||
final tNextProps = factory(nextProps); | ||
return areEqual(tPrevProps, tNextProps); | ||
} | ||
|
||
hoc = react_interop.memo(factory().componentFactory, areEqual: wrapProps); | ||
} else { | ||
hoc = react_interop.memo(factory().componentFactory); | ||
} | ||
|
||
setComponentTypeMeta(hoc, | ||
isHoc: true, parentType: factory().componentFactory); | ||
|
||
TProps forwardedFactory([Map props]) { | ||
return factory(props)..componentFactory = hoc; | ||
} | ||
|
||
return forwardedFactory; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright 2019 Workiva Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
library memo_test; | ||
|
||
import 'package:over_react/src/util/memo.dart'; | ||
import 'package:over_react_test/over_react_test.dart'; | ||
import 'package:test/test.dart'; | ||
import 'package:over_react/over_react.dart'; | ||
import 'fixtures/basic_ui_component.dart'; | ||
|
||
part 'memo_test.over_react.g.dart'; | ||
|
||
int renderCount = 0; | ||
mixin FunctionCustomPropsProps on UiProps { | ||
int testProp; | ||
} | ||
|
||
UiFactory<FunctionCustomPropsProps> FunctionCustomProps = uiFunction( | ||
(props) { | ||
renderCount++; | ||
return Dom.div()(Dom.div()('prop id: ${props.id}'), | ||
Dom.div()('test Prop: ${props.testProp}')); | ||
}, | ||
$FunctionCustomPropsConfig, // ignore: undefined_identifier | ||
); | ||
|
||
main() { | ||
group('memo -', () { | ||
test('errors when wrapping a UiComponent', () { | ||
expect(() => memo<BasicUiComponentProps>(BasicUiComponent), | ||
throwsArgumentError); | ||
}); | ||
|
||
test('errors when wrapping a UiComponent2', () { | ||
expect(() => memo<BasicUiComponent2Props>(BasicUiComponent2), | ||
throwsArgumentError); | ||
}); | ||
|
||
test('wraps a function component', () { | ||
UiFactory<UiProps> FunctionTest = uiFunction( | ||
(props) { | ||
return Dom.div()('prop id: ${props.id}'); | ||
}, | ||
FunctionComponentConfig(), | ||
); | ||
|
||
UiFactory<UiProps> FunctionMemo = memo<UiProps>(FunctionTest); | ||
mount(FunctionMemo()()); | ||
|
||
expect(isValidElementOfType(FunctionMemo()(), FunctionTest), isTrue); | ||
}); | ||
|
||
test('wraps a function component with custom props', () { | ||
UiFactory<FunctionCustomPropsProps> FunctionCustomPropsMemo = | ||
memo<FunctionCustomPropsProps>(FunctionCustomProps); | ||
mount(FunctionCustomPropsMemo()()); | ||
|
||
expect( | ||
isValidElementOfType( | ||
FunctionCustomPropsMemo()(), FunctionCustomProps), | ||
isTrue); | ||
}); | ||
|
||
test('memoizes based on props', () { | ||
renderCount = 0; | ||
UiFactory<FunctionCustomPropsProps> FunctionCustomPropsMemo = | ||
memo<FunctionCustomPropsProps>(FunctionCustomProps); | ||
final testJacket = mount((FunctionCustomPropsMemo()..testProp = 1)()); | ||
testJacket.rerender((FunctionCustomPropsMemo()..testProp = 1)()); | ||
testJacket.rerender((FunctionCustomPropsMemo()..testProp = 1)()); | ||
|
||
expect(renderCount, equals(1)); | ||
}); | ||
|
||
test('memoizes based on areEqual parameter', () { | ||
renderCount = 0; | ||
UiFactory<FunctionCustomPropsProps> FunctionCustomPropsMemo = | ||
memo<FunctionCustomPropsProps>(FunctionCustomProps, | ||
areEqual: ((prevProps, nextProps) { | ||
return prevProps.testProp == nextProps.testProp - 1; | ||
})); | ||
final testJacket = mount((FunctionCustomPropsMemo()..testProp = 1)()); | ||
testJacket.rerender((FunctionCustomPropsMemo()..testProp = 2)()); | ||
testJacket.rerender((FunctionCustomPropsMemo()..testProp = 4)()); | ||
|
||
expect(renderCount, equals(2)); | ||
}); | ||
}); | ||
} | ||
|
||
@Factory() | ||
UiFactory<BasicUiComponent2Props> BasicUiComponent2 = _$BasicUiComponent2; | ||
|
||
@Props() | ||
class _$BasicUiComponent2Props extends UiProps { | ||
String childId; | ||
} | ||
|
||
@Component2() | ||
class BasicUiComponent2Component extends UiComponent2<BasicUiComponent2Props> { | ||
@override | ||
render() => 'basic component'; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#nit there's just some lints from this (docs ref here):