Skip to content
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-8665 Expose React Hooks #611

Merged
merged 11 commits into from
Jul 29, 2020

Conversation

sydneyjodon-wk
Copy link
Contributor

@sydneyjodon-wk sydneyjodon-wk commented Jul 25, 2020

Motivation

React hooks were implemented in react-dart and we need to expose them in over_react.

Changes

  • Expose and export hooks.
  • Update doc comment examples to over_react from react-dart examples.
  • Add hooks to example.

Release Notes

Review

See CONTRIBUTING.md for more details on review types (+1 / QA +1 / +10) and code review process.

Please review:

QA Checklist

  • Tests were updated and provide good coverage of the changeset and other affected code
  • Manual testing was performed if needed

Merge Checklist

While we perform many automated checks before auto-merging, some manual checks are needed:

  • A Client Platform member has reviewed these changes
  • There are no unaddressed comments - this check can be automated if reviewers use the "Request Changes" feature
  • For release PRs - Version metadata in Rosie comment is correct

@aviary-wf
Copy link

aviary-wf commented Jul 25, 2020

Security Insights

No security relevant content was detected by automated scans.

Action Items

  • Review PR for security impact; comment "security review required" if needed or unsure
  • Verify aviary.yaml coverage of security relevant code

Questions or Comments? Reach out on Slack: #support-infosec.

);

//final UseContextTest = uiFunction<UiProps>((props) {
// final context = useContext(TestNewContext);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is part of this ticket, but this example doesn't work. I think it has something to do with the react-dart version of useContext returning React.context and here it returns the over_react version of Context. It also could have to do with me not knowing much about how Context works and converting the react-dart example wrong.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to get to the bottom of this, I think! Looks like there was some missing typing on some of the calls into react-dart's context, unrelated to hooks.

The following changes got it working for me:

diff --git a/lib/src/util/context.dart b/lib/src/util/context.dart
index 40f7e7c5..2299e74f 100644
--- a/lib/src/util/context.dart
+++ b/lib/src/util/context.dart
@@ -49,7 +49,7 @@ class Context<TValue> {
   Context(this.Provider, this.Consumer, this.reactDartContext);
 
   /// The react.dart version of this context.
-  final react.Context reactDartContext;
+  final react.Context<TValue> reactDartContext;
 
   /// The react.js version of this context.
   ReactContext get jsThis => reactDartContext.jsThis;
@@ -237,7 +237,7 @@ class _DO_NOT_USE_OR_YOU_WILL_BE_FIRED {
 ///
 /// Learn more: <https://reactjs.org/docs/context.html#reactcreatecontext>
 Context<TValue> createContext<TValue>([TValue defaultValue, int Function(TValue, TValue) calculateChangedBits]) {
-  react.Context reactDartContext = react.createContext(defaultValue, calculateChangedBits != null ? (dynamic arg1, dynamic arg2) => calculateChangedBits(arg1, arg2) : null);
+  final reactDartContext = react.createContext<TValue>(defaultValue, calculateChangedBits != null ? (dynamic arg1, dynamic arg2) => calculateChangedBits(arg1, arg2) : null);
   // ignore: prefer_function_declarations_over_variables, avoid_types_on_closure_parameters
   UiFactory<ProviderProps> Provider = ([Map map]) => (ProviderProps<TValue>(map)..componentFactory = reactDartContext.Provider);
   // ignore: prefer_function_declarations_over_variables, avoid_types_on_closure_parameters

Are you cool just including those fixes in this PR? I think they're pretty low-risk, and are definitely considered bug fixes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay!!! Thank you! I'll add that fix

Comment on lines 50 to 52
FunctionComponentConfig(
displayName: 'UseStateTest',
),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want to suggest to consumers to manually create component configs and specify displayNames; could we update these components to have props mixins so that they get generated configs?

That makes me wonder; should we, in the future, generate configs even for function components with UiProps? That way they'd get displayName without having to generate it, and it the syntax would be the same for components that do and don't have their own props classes, making it easier to switch between the two.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea! It would make the function component boilerplate a lot more consistent if you only had to create a config when you want to specify your own propsFactory.

Comment on lines 78 to 79
final ReducerHook<Map, Map, int> state =
useReducerLazy(reducer, props.initialCount, initializeCount);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#nit could we use RHS typing here?

Also, I feel like "state" might be a bit confusing, but I'm not sure what a better word would be.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I also don't know what else to name it, but I agree that it's not great

);

//final UseContextTest = uiFunction<UiProps>((props) {
// final context = useContext(TestNewContext);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to get to the bottom of this, I think! Looks like there was some missing typing on some of the calls into react-dart's context, unrelated to hooks.

The following changes got it working for me:

diff --git a/lib/src/util/context.dart b/lib/src/util/context.dart
index 40f7e7c5..2299e74f 100644
--- a/lib/src/util/context.dart
+++ b/lib/src/util/context.dart
@@ -49,7 +49,7 @@ class Context<TValue> {
   Context(this.Provider, this.Consumer, this.reactDartContext);
 
   /// The react.dart version of this context.
-  final react.Context reactDartContext;
+  final react.Context<TValue> reactDartContext;
 
   /// The react.js version of this context.
   ReactContext get jsThis => reactDartContext.jsThis;
@@ -237,7 +237,7 @@ class _DO_NOT_USE_OR_YOU_WILL_BE_FIRED {
 ///
 /// Learn more: <https://reactjs.org/docs/context.html#reactcreatecontext>
 Context<TValue> createContext<TValue>([TValue defaultValue, int Function(TValue, TValue) calculateChangedBits]) {
-  react.Context reactDartContext = react.createContext(defaultValue, calculateChangedBits != null ? (dynamic arg1, dynamic arg2) => calculateChangedBits(arg1, arg2) : null);
+  final reactDartContext = react.createContext<TValue>(defaultValue, calculateChangedBits != null ? (dynamic arg1, dynamic arg2) => calculateChangedBits(arg1, arg2) : null);
   // ignore: prefer_function_declarations_over_variables, avoid_types_on_closure_parameters
   UiFactory<ProviderProps> Provider = ([Map map]) => (ProviderProps<TValue>(map)..componentFactory = reactDartContext.Provider);
   // ignore: prefer_function_declarations_over_variables, avoid_types_on_closure_parameters

Are you cool just including those fixes in this PR? I think they're pretty low-risk, and are definitely considered bug fixes

/// __Example__:
///
/// ```dart
/// UiFactory<UiProps> UseStateExample = uiFunction(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comnments as in examples around formatting and using generated function configs. I think here we can omit the props mixin declarations themselves, though, and just use them in the typing.

@sydneyjodon-wk sydneyjodon-wk changed the base branch from CPLAT-3873-function-components to function-components-wip July 28, 2020 16:50
@sydneyjodon-wk sydneyjodon-wk dismissed greglittlefield-wf’s stale review July 29, 2020 15:38

All feedback addressed

Copy link
Contributor

@greglittlefield-wf greglittlefield-wf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+10

Address nits

Co-authored-by: Greg Littlefield <[email protected]>
// 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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my bad, missing an import now 😬

Suggested change
import 'dart:html';

final width = useState(0);
final height = useState(0);

final textareaRef = useRef<TextareaElement>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
final textareaRef = useRef<TextareaElement>();
final textareaRef = useRef<TextAreaElement>();

Whoops again 😓

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in a168750

Copy link
Contributor

@greglittlefield-wf greglittlefield-wf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+10

@greglittlefield-wf greglittlefield-wf merged commit 5ca5761 into function-components-wip Jul 29, 2020
@sydneyjodon-wk sydneyjodon-wk deleted the CPLAT-8665-react-hooks branch January 28, 2022 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants