Skip to content

Commit

Permalink
compose: Type withState as any
Browse files Browse the repository at this point in the history
  • Loading branch information
sarayourfriend committed Jun 13, 2021
1 parent e1f772e commit 112ff78
Showing 1 changed file with 8 additions and 35 deletions.
43 changes: 8 additions & 35 deletions packages/compose/src/higher-order/with-state/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/**
* External dependencies
*/
// eslint-disable-next-line no-restricted-imports
import type { ComponentType } from 'react';

/**
* WordPress dependencies
*/
Expand All @@ -14,8 +8,6 @@ import deprecated from '@wordpress/deprecated';
* Internal dependencies
*/
import createHigherOrderComponent from '../../utils/create-higher-order-component';
// eslint-disable-next-line no-duplicate-imports
import type { HigherOrderComponent } from '../../utils/create-higher-order-component';

/**
* A Higher Order Component used to provide and manage internal component state
Expand All @@ -27,37 +19,18 @@ import type { HigherOrderComponent } from '../../utils/create-higher-order-compo
*
* @return A higher order component wrapper accepting a component that takes the state props + its own props + `setState` and returning a component that only accepts the own props.
*/
export default function withState<
TProps extends object,
TStateProps extends object
>(
initialState: TStateProps = {} as TStateProps
): HigherOrderComponent<
TProps &
TStateProps & {
setState: Component< TProps, TStateProps >[ 'setState' ];
},
TProps
> {
export default function withState(
initialState: any
): any {
deprecated( 'wp.compose.withState', {
alternative: 'wp.element.useState',
} );

return createHigherOrderComponent(
(
OriginalComponent: ComponentType<
TProps &
TStateProps & {
setState: Component<
TProps,
TStateProps
>[ 'setState' ];
}
>
OriginalComponent: any
) => {
return class WrappedComponent extends Component<
TProps,
TStateProps
> {
return class WrappedComponent extends Component {
constructor( props: any ) {
super( props );

Expand All @@ -69,8 +42,8 @@ export default function withState<
render() {
return (
<OriginalComponent
{ ...( this.props as TProps ) }
{ ...( this.state as TStateProps ) }
{ ...this.props }
{ ...this.state }
setState={ this.setState }
/>
);
Expand Down

0 comments on commit 112ff78

Please sign in to comment.