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

[sitecore-jss-react] Refactored withComponentFactory HOC #1086

Merged
merged 2 commits into from
Jul 7, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions packages/sitecore-jss-react/src/enhancers/withComponentFactory.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { ComponentFactoryReactContext } from '../components/SitecoreContext';
import { ComponentFactory } from '../components/sharedTypes';
import { useContext } from 'react';

export interface ComponentFactoryProps {
componentFactory?: ComponentFactory;
Expand All @@ -12,11 +13,22 @@ export interface ComponentFactoryProps {
export function withComponentFactory<T extends ComponentFactoryProps>(
Component: React.ComponentClass<T> | React.FC<T>
) {
return function WithComponentFactory(props: T) {
/**
* @param {T} props - props to pass to the wrapped component
* @returns {JSX.Element} - the rendered component
*/
function WithComponentFactory(props: T) {
const context = useContext(ComponentFactoryReactContext);
return (
<ComponentFactoryReactContext.Consumer>
addy-pathania marked this conversation as resolved.
Show resolved Hide resolved
{(context) => <Component {...props} componentFactory={props.componentFactory || context} />}
{() => <Component {...props} componentFactory={props.componentFactory || context} />}
</ComponentFactoryReactContext.Consumer>
);
};
}

WithComponentFactory.displayName = `withComponentFactory(${Component.displayName ||
Component.name ||
'Anonymous'})`;

return WithComponentFactory;
}