-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Can we get some clear guidance on HOCs? #29175
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
Comments
export interface FormContext<DataType> {
form: FormContextUtilities<DataType>;
immutable: boolean;
index: number;
data: DataType;
validationErrors: ValidationErrors<DataType>;
schema?: Yup.Schema<DataItemType<DataType>>;
}
export function withForm
<
DataType,
ContextType extends UsesFormContext<DataType>,
ComponentType extends React.ComponentType<ContextType>,
FormComponentProps extends GetProps<ComponentType>,
WrappedComponentProps extends Omit<FormComponentProps, keyof ContextType>
>
// ITEM 1
(FormComponent: React.ComponentType<FormComponentProps>): React.ComponentType<WrappedComponentProps> {
return (props: WrappedComponentProps) => <FormConsumer>
{
// ITEM 2
(context: UsesFormContext<DataType>) => <FormComponent
{...context as any}
{...props as any}
/>
}
</FormConsumer>
} Item 1One way I can make this issue go away and - seemingly - get the desired outcome is to do this for my HOC factory signature:
I'm not going to lie, the errors I'm getting back have me in "guess and check" mode right now. Not entirely sure what a correct solution would be in this situation. Item 2If I remove the
|
@atrauzzi this would be the way to do it, unfortunately there's a current bug that prevents the spread props from being recombined properly: import React from 'react'
declare const _: {
mapValues<T extends Record<string, any>, U>(obj: T, mapFn: (val: T[keyof T]) => U): Record<keyof T, U>
}
export function reactInjectDictionary<
Props,
InjectedKeys extends keyof Props
>
(
ComponentType: React.JSXElementConstructor<Props>,
injectionMap: Record<InjectedKeys, symbol>
) {
// ITEM 1
return function ComponentWithInjection(props: Omit<Props, InjectedKeys>) {
return <BundleConsumer>
{
(bundle) => {
if (!bundle) {
throw new Error("This portion of the React component graph is built using protoculture-react-native. Please be sure to wrap it with a `BundleProvider`.");
}
// ITEM 2
const injections: Pick<Props, InjectedKeys> = _.mapValues(injectionMap, (symbol) =>
bundle.container.get(symbol));
return <ComponentType
{...props as Props}
{...injections}
/>
}
}
</BundleConsumer>;
};
} |
the |
Note the annotation for |
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow. |
Update: I'm quite certain I'm not alone when I say I'm at my limit with this.
I'm aware that there are a significant number of tickets where people are attempting to come up with correct typings for their React higher order components.
That problem appears to be solved by the community with the types I'm including in the first two lines of my snippet- however my problemdiffers slightly...I'm defining an HOC that excludes based on the keys provided to the second parameter of my factory. You'll see this in my example as
injectionMap
. Its usage looks like this:The idea here is that any key in the dictionary should:
MyComponent
From what I can see, I might actually have the second item working, but I have some issues which are still causing errors that don't make sense:
ITEM 1
ITEM 2
Some of this feels like it's starting to work, but there are just the few snags I've outlined above which make no sense to me based on what I'm being told. My rationale is this:
Props
because the type ofInjectedKeys
is derived fromProps
viaInjectionMap
, which in turn is derived fromPropsKey
.InjectionMap
is a statically defined dictionary:{[key in PropsKey]: symbol}
It could be that I'm running afoul of something the type system knows that I don't, but the way it's being conveyed contradicts some of how it's coming together as well. The overall goal here is that any key provided as that second parameter to
reactInjectDictionary
should be subtracted from the resultantProps
signature of the generated HOC.cc.
Readonly
intersections #27484The text was updated successfully, but these errors were encountered: