UNPKG

@wordpress/compose

Version:
51 lines (48 loc) 1.65 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = withState; var _element = require("@wordpress/element"); var _deprecated = _interopRequireDefault(require("@wordpress/deprecated")); var _createHigherOrderComponent = require("../../utils/create-higher-order-component"); var _jsxRuntime = require("react/jsx-runtime"); /** * WordPress dependencies */ /** * Internal dependencies */ /** * 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. */function withState(initialState = {}) { (0, _deprecated.default)('wp.compose.withState', { since: '5.8', alternative: 'wp.element.useState' }); return (0, _createHigherOrderComponent.createHigherOrderComponent)(OriginalComponent => { return class WrappedComponent extends _element.Component { constructor(/** @type {any} */props) { super(props); this.setState = this.setState.bind(this); this.state = initialState; } render() { return /*#__PURE__*/(0, _jsxRuntime.jsx)(OriginalComponent, { ...this.props, ...this.state, setState: this.setState }); } }; }, 'withState'); } //# sourceMappingURL=index.js.map