Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: Extends docs for withFilters HOC #11313

Merged
merged 1 commit into from
Oct 31, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 47 additions & 12 deletions packages/components/src/higher-order/with-filters/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,60 @@ Wrapping a component with `withFilters` provides a filtering capability controll
## Usage

```jsx
import { withFilters } from '@wordpress/components';
import { Fragment, withFilters } from '@wordpress/components';
import { addFilter } from '@wordpress/hooks';

const ComposedComponent = () => <div>Composed component</div>;
const MyComponent = ( { title } ) => <h1>{ title }</h1>;

const ComponentToAppend = () => <div>Appended component</div>;

function withComponentApended( FilteredComponent ) {
return ( props ) => (
<Fragment>
<FilteredComponent { ...props } />
<ComponentToAppend />
</Fragment>
);
}

addFilter(
'MyHookName',
'example/filtered-component',
( FilteredComponent ) => () => (
<div>
<FilteredComponent />
<ComposedComponent />
</div>
)
'my-plugin/with-component-appended',
withComponentApended
);

const MyComponentWithFilters = withFilters( 'MyHookName' )(
() => <div>My component</div>
);
const MyComponentWithFilters = withFilters( 'MyHookName' )( MyComponent );
```

`withFilters` expects a string argument which provides a hook name. It returns a function which can then be used in composing your component. The hook name allows plugin developers to customize or completely override the component passed to this higher-order component using `wp.hooks.addFilter` method.

It is also possible to override props by implementing a higher-order component which works as follows:

```jsx
import { Fragment, withFilters } from '@wordpress/components';
import { addFilter } from '@wordpress/hooks';

const MyComponent = ( { hint, title } ) => (
<Fragment>
<h1>{ title }</h1>
<p>{ hint }</p>
</Fragment>
);

function withHintOverriden( FilteredComponent ) {
return ( props ) => (
<FilteredComponent
{ ...props }
hint="Overriden hint"
/>
);
}

addFilter(
'MyHookName',
'my-plugin/with-hint-overriden',
withHintOverriden
);

const MyComponentWithFilters = withFilters( 'MyHookName' )( MyComponent );
```