Gutenberg: withFilters: Filter is not applied

Created on 5 Aug 2018  路  8Comments  路  Source: WordPress/gutenberg

Describe the bug
When rendering a component using the withFilters higher order component outside Gutenberg, the filter is not applied.

To Reproduce
Execute the code below in a new React project after installing @wordpress/components and @wordpress/hooks.

import { withFilters } from '@wordpress/components';
import { addFilter } from '@wordpress/hooks';

const ComposedComponent = () => <div>Composed component</div>;

addFilter(
    'MyHookName',
    'example/filtered-component',
    ( FilteredComponent ) => () => (
        <div>
            <FilteredComponent />
            <ComposedComponent />
        </div>
    )
);

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

ReactDOM.render(
    <MyComponentWithFilters />,
    document.getElementById( 'root' )
);

Expected behavior
"Composed component" should appear below "My component".

Screenshots
screen shot 2018-08-05 at 23 53 56

Desktop:

  • OS: macOS High Sierra
  • Browser: Chrome
  • Version: 67

Additional context
Issue found while working on #8338 and Automattic/wp-calypso#26367

Framework [Package] Components [Type] Bug

Most helpful comment

@jsnajdr, thanks for double checking this one. I'm closing this issue in here, because it isn't an issue with hooks itself. We will try to come up with an action item to attack this general issue of consuming multiple @wordpress/* packages at once tomorrow during Core JS meeting.
See: https://docs.google.com/document/d/1KNa4xxktVskFV86SVMbdTWx3bINrLikBnJpntBZw1DU/edit.

All 8 comments

I can't reproduce inside Gutenberg:

screen shot 2018-08-06 at 15 24 18

Can you try again outside of LiveReact context and confirm?

you're right! react-live is causing the issue. I have tested it in a clean project and is working nicely:

I'll try to figure out how to solve the issue with react-live.

sorry for false alarm!

Don鈥檛 you encounter a similar issue as with data package where 2 instances of the same library with different versions are loaded?

I think you're confusing with someone else. I have not played around yet with the data package.

It was a question, see a related discussion where two different versions of the same library were loaded causing issues with internal state: https://github.com/Automattic/wp-calypso/pull/26438#issuecomment-411341882

@jsnajdr can you help to debug this one?

Yes, it looks like the @wordpress/hooks module is duplicated and because it contains a filter registry, the addFilter and applyFilters function work with different registries.

On my local wp-calypso, I have 5 copies:

$ find . -path "*/@wordpress/hooks"
./node_modules/@wordpress/blocks/node_modules/@wordpress/hooks
./node_modules/@wordpress/api-fetch/node_modules/@wordpress/hooks
./node_modules/@wordpress/components/node_modules/@wordpress/hooks
./node_modules/@wordpress/hooks
./node_modules/@wordpress/editor/node_modules/@wordpress/hooks

The solution is to regenerate the npm-shrinkwrap.json file with npm run update-deps. That forces NPM to deduplicate the modules. It's also important that @wordpress/hooks is a top level dependency of Calypso, but it already is.

@jsnajdr, thanks for double checking this one. I'm closing this issue in here, because it isn't an issue with hooks itself. We will try to come up with an action item to attack this general issue of consuming multiple @wordpress/* packages at once tomorrow during Core JS meeting.
See: https://docs.google.com/document/d/1KNa4xxktVskFV86SVMbdTWx3bINrLikBnJpntBZw1DU/edit.

Was this page helpful?
0 / 5 - 0 ratings