Skip to content

Commit

Permalink
Compose: Deprecate withState in favor of useState (#32368)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka authored Jun 1, 2021
1 parent 0a6ac7f commit e6ff80f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/compose/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Deprecations

- `withState` HOC has been deprecated. Use `useState` hook instead.

## 4.1.0 (2021-05-20)

## 4.0.0 (2021-05-14)
Expand Down
2 changes: 2 additions & 0 deletions packages/compose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,8 @@ _Returns_

<a name="withState" href="#withState">#</a> **withState**

> **Deprecated** Use `useState` instead.
A Higher Order Component used to provide and manage internal component state
via props.

Expand Down
2 changes: 2 additions & 0 deletions packages/compose/src/higher-order/with-state/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# withState

**Deprecated**

`withState` is a React [higher-order component](https://facebook.github.io/react/docs/higher-order-components.html) which enables a function component to have internal state.

Wrapping a component with `withState` provides state as props to the wrapped component, along with a `setState` updater function.
Expand Down
7 changes: 7 additions & 0 deletions packages/compose/src/higher-order/with-state/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import deprecated from '@wordpress/deprecated';

/**
* Internal dependencies
Expand All @@ -12,11 +13,17 @@ import createHigherOrderComponent from '../../utils/create-higher-order-componen
* A Higher Order Component used to provide and manage internal component state
* via props.
*
* @deprecated Use `useState` instead.
*
* @param {?Object} initialState Optional initial state of the component.
*
* @return {WPComponent} Wrapped component.
*/
export default function withState( initialState = {} ) {
deprecated( 'wp.compose.withState', {
alternative: 'wp.element.useState',
} );

return createHigherOrderComponent( ( OriginalComponent ) => {
return class WrappedComponent extends Component {
constructor() {
Expand Down
2 changes: 2 additions & 0 deletions packages/compose/src/higher-order/with-state/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ describe( 'withState', () => {
const wrapper = TestUtils.renderIntoDocument(
getTestComponent( EnhancedComponent )
);

const buttonElement = () =>
TestUtils.findRenderedDOMComponentWithTag( wrapper, 'button' );

expect( console ).toHaveWarned();
expect( buttonElement().outerHTML ).toBe( '<button>0</button>' );
TestUtils.Simulate.click( buttonElement() );
expect( buttonElement().outerHTML ).toBe( '<button>1</button>' );
Expand Down

0 comments on commit e6ff80f

Please sign in to comment.