@gechiui/compose
Version:
GeChiUI higher-order components (HOCs).
50 lines (43 loc) • 1.25 kB
JavaScript
/**
* GeChiUI dependencies
*/
import { Component } from '@gechiui/element';
import deprecated from '@gechiui/deprecated';
/**
* Internal dependencies
*/
import createHigherOrderComponent from '../../utils/create-higher-order-component';
/**
* A Higher Order Component used to provide and manage internal component state
* via props.
*
* @deprecated Use `useState` instead.
*
* @param {any} initialState Optional initial state of the component.
*
* @return {any} 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( initialState = {} ) {
deprecated( 'gc.compose.withState', {
since: '5.8',
alternative: 'gc.element.useState',
} );
return createHigherOrderComponent( ( OriginalComponent ) => {
return class WrappedComponent extends Component {
constructor( /** @type {any} */ props ) {
super( props );
this.setState = this.setState.bind( this );
this.state = initialState;
}
render() {
return (
<OriginalComponent
{ ...this.props }
{ ...this.state }
setState={ this.setState }
/>
);
}
};
}, 'withState' );
}