Skip to content

CPLAT-8036 Add useContext Hook #237

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

Merged
merged 8 commits into from
Dec 19, 2019

Conversation

joebingham-wk
Copy link
Collaborator

@joebingham-wk joebingham-wk commented Dec 5, 2019

Motivation

Expose the useContext hook so that functional components can tap into it.

Changes

  • Add useContext function
  • Add an example
  • Add tests

Testing

  • Verify the example works as expected

Please Review @greglittlefield-wf @kealjones-wk @aaronlademann-wf @sydneyjodon-wk

@aaronlademann-wf aaronlademann-wf changed the base branch from 5.2.0-wip to 5.3.0-wip December 11, 2019 22:43
react.registerFunctionComponent(UseContextTestComponent, displayName: 'useContextTest');

UseContextTestComponent(Map props) {
final context = useContext(TestNewContext);
Copy link
Collaborator

Choose a reason for hiding this comment

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

It bothers me a bit that context will be dynamic unless you provide a type on the left side, and even then - there'd be no way to ensure that the type doesn't mismatch with the underlying type that was returned by createContext. Makes me wonder what the point of the generic parameter is on createContext.

By making these changes locally... all the typing seems to line up and makes context correctly inferred as a Map in this situation as a result of createContext<Map> being called on line 63.

// context.dart

- class Context {
+ class Context<T> {
// ...
}

- Context createContext<TValue>([
+ Context<TValue> createContext<TValue>([
// ...
// hooks.dart

- dynamic useContext(Context context) => // ...
+ T useContext<T>(Context<T> context) => // ...

@greglittlefield-wf @kealjones-wk @joebingham-wk is there some reason that I forgot about that led us to omit generic typing from the Context class?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Honestly I don't recall, I feel like there would have been a reason because the Context object in OverReact is typed exactly like that and i don't know why i would have omitted it in React-Dart unless it was just a minor oversight (very likely). @joebingham-wk if you can add this typing id say do it, but test the local context examples in browser and test it with over_react (tests & real examples if already in place) to make sure it doesn't break anything (i don't think it should).

Copy link
Collaborator

@aaronlademann-wf aaronlademann-wf left a comment

Choose a reason for hiding this comment

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

+1

Co-Authored-By: Greg Littlefield <[email protected]>
Copy link
Collaborator

@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 7cf33d8 into 5.3.0-wip Dec 19, 2019
@aaronlademann-wf aaronlademann-wf added this to the 5.4.0 milestone Jan 29, 2020
@greglittlefield-wf greglittlefield-wf deleted the CPLAT-8036-useContext-hook branch February 16, 2022 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants