Files
outline/app/components/withStores.tsx
Tom Moor 15b1069bcc chore: Move to Typescript (#2783)
This PR moves the entire project to Typescript. Due to the ~1000 ignores this will lead to a messy codebase for a while, but the churn is worth it – all of those ignore comments are places that were never type-safe previously.

closes #1282
2021-11-29 06:40:55 -08:00

35 lines
1020 B
TypeScript

import hoistNonReactStatics from "hoist-non-react-statics";
import * as React from "react";
import RootStore from "~/stores/RootStore";
import useStores from "~/hooks/useStores";
type StoreProps = keyof RootStore;
function withStores<
P extends React.ComponentType<React.ComponentProps<P> & RootStore>,
ResolvedProps = JSX.LibraryManagedAttributes<
P,
Omit<React.ComponentProps<P>, StoreProps>
>
>(WrappedComponent: P): React.FC<Omit<ResolvedProps, StoreProps>> {
const ComponentWithStore = (
props: Omit<React.ComponentProps<P>, StoreProps>
) => {
const stores = useStores();
return <WrappedComponent {...(props as any)} {...stores} />;
};
ComponentWithStore.displayName = `WithStores(${
WrappedComponent.name || WrappedComponent.displayName
})`;
/**
* https://reactjs.org/docs/higher-order-components.html#static-methods-must-be-copied-over
*/
hoistNonReactStatics(ComponentWithStore, WrappedComponent);
return ComponentWithStore;
}
export default withStores;