-
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-8665 Expose React Hooks #611
CPLAT-8665 Expose React Hooks #611
Conversation
Security InsightsNo security relevant content was detected by automated scans. Action Items
Questions or Comments? Reach out on Slack: #support-infosec. |
); | ||
|
||
//final UseContextTest = uiFunction<UiProps>((props) { | ||
// final context = useContext(TestNewContext); |
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.
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.
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.
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
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.
Yay!!! Thank you! I'll add that fix
FunctionComponentConfig( | ||
displayName: 'UseStateTest', | ||
), |
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.
I don't think we want to suggest to consumers to manually create component configs and specify displayName
s; 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.
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.
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
.
final ReducerHook<Map, Map, int> state = | ||
useReducerLazy(reducer, props.initialCount, initializeCount); |
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 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.
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.
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); |
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.
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
lib/src/component/hooks.dart
Outdated
/// __Example__: | ||
/// | ||
/// ```dart | ||
/// UiFactory<UiProps> UseStateExample = uiFunction( |
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.
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.
All feedback addressed
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.
+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. | ||
|
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.
my bad, missing an import now 😬
import 'dart:html'; | |
final width = useState(0); | ||
final height = useState(0); | ||
|
||
final textareaRef = useRef<TextareaElement>(); |
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.
final textareaRef = useRef<TextareaElement>(); | |
final textareaRef = useRef<TextAreaElement>(); |
Whoops again 😓
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.
fixed in a168750
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.
+10
Motivation
React hooks were implemented in react-dart and we need to expose them in over_react.
Changes
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
pbr serve example
and verify that examples work in http://localhost:8080/hooks/Merge Checklist
While we perform many automated checks before auto-merging, some manual checks are needed: