带过滤器

带过滤器是的一部分本地古腾堡扩展性它也是一种反应高阶分量.

用包装组件带过滤器提供由外部控制的过滤功能挂钩名称.

用法

从“@wordpress/components”导入{withFilters};从“@wordpress/hooks”导入{addFilter};const MyComponent=({title})=><h1>{titleneneneep;const ComponentToAppend=()=>附加组件;带有ComponentAppended(FilteredComponent)的函数{返回(道具)=>(<><过滤组件{…props}/><组件附加/></>);}添加筛选器(“MyHookName”,“my-plugin/with-component-appended”,已附加组件);const MyComponentWithFilters=带过滤器('MyHookName')(MyComponent);

带过滤器需要提供挂钩名称的字符串参数。它返回一个函数,然后可以使用该函数组合组件。钩子名称允许插件开发人员使用wp.hooks.add过滤器方法。

还可以通过实现一个高阶组件来覆盖道具,该组件的工作原理如下:

从“@wordpress/components”导入{withFilters};从“@wordpress/hooks”导入{addFilter};const MyComponent=({hint,title})=>(<><h1>{标题}</h1><p>{提示}</p></>);带有HintOverrided(FilteredComponent)的函数{返回(道具)=>(<FilteredComponent{…props}hint=“覆盖的提示”/>);}addFilter('MyHookName','my-plugin/with-hint-overridden',withHintOverrided);const MyComponentWithFilters=带过滤器('MyHookName')(MyComponent);