最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - Gutenberg ServerSideRender is deprecated, how to work with new wp.serverSideRender component?

programmeradmin2浏览0评论

While working with ServerSideRender wordpress gutenberg component, I am seeing the following notice.

wpponents.ServerSideRender is deprecated. Please use wp.serverSideRender instead.

Here is my test code:

const { __ } = wp.i18n;
const { registerBlockType } = wp.blocks;
const { Fragment } = wp.element;

const { InspectorControls } = wp.blockEditor;

const {
    PanelBody, SelectControl, ServerSideRender, TextControl, ToggleControl,
} = wpponents;

registerBlockType( 'test-me/test-to-block', {
    title: __( 'Test Me' ),
    icon: 'shield',
    category: 'common',
    keywords: [
        __( 'Test Me' ),
        __( 'Test' ),
        __( 'msbd' ),
    ],

    attributes: {
        adAlignment: {
            type: 'string',
            default: 'center-align',
        },
        isWrapper: {
            type: 'boolean',
            default: true,
        },
    },

    edit: ( props ) => {
        const { setAttributes, attributes } = props;
        const { adAlignment, isWrapper } = attributes;

        return (
            <Fragment>
                <div className={ `test-me ${ className }` }>
                    <ServerSideRender
                        block="test-me/test-to-block"
                        attributes={ attributes }
                    />
                </div>
            </Fragment>
        );
    },

    save: ( props ) => {
        const { attributes, className = '' } = props;

        return (
            <div className={ `test-me ${ className }` }>
                <ServerSideRender
                    block="test-me/test-to-block"
                    attributes={ attributes }
                />
            </div>
        );
    },
} );

How to modify this script to work with new wp.serverSideRender component?

While working with ServerSideRender wordpress gutenberg component, I am seeing the following notice.

wpponents.ServerSideRender is deprecated. Please use wp.serverSideRender instead.

Here is my test code:

const { __ } = wp.i18n;
const { registerBlockType } = wp.blocks;
const { Fragment } = wp.element;

const { InspectorControls } = wp.blockEditor;

const {
    PanelBody, SelectControl, ServerSideRender, TextControl, ToggleControl,
} = wpponents;

registerBlockType( 'test-me/test-to-block', {
    title: __( 'Test Me' ),
    icon: 'shield',
    category: 'common',
    keywords: [
        __( 'Test Me' ),
        __( 'Test' ),
        __( 'msbd' ),
    ],

    attributes: {
        adAlignment: {
            type: 'string',
            default: 'center-align',
        },
        isWrapper: {
            type: 'boolean',
            default: true,
        },
    },

    edit: ( props ) => {
        const { setAttributes, attributes } = props;
        const { adAlignment, isWrapper } = attributes;

        return (
            <Fragment>
                <div className={ `test-me ${ className }` }>
                    <ServerSideRender
                        block="test-me/test-to-block"
                        attributes={ attributes }
                    />
                </div>
            </Fragment>
        );
    },

    save: ( props ) => {
        const { attributes, className = '' } = props;

        return (
            <div className={ `test-me ${ className }` }>
                <ServerSideRender
                    block="test-me/test-to-block"
                    attributes={ attributes }
                />
            </div>
        );
    },
} );

How to modify this script to work with new wp.serverSideRender component?

Share Improve this question asked Dec 2, 2019 at 13:00 Shah AlomShah Alom 4981 gold badge5 silver badges14 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 9

The component is still functional, the issue is due to how it was pulled out into its own package. It is no longer uppercase and in order to use it in JSX, you will need to alias it:

const { serverSideRender: ServerSideRender } = wp;

See this issue for more details.

This just means they moved the component. Replace this part:

const {
    PanelBody, SelectControl, ServerSideRender, TextControl, ToggleControl,
} = wpponents;

with

const { serverSideRender } = wp;
const {
    PanelBody, SelectControl, TextControl, ToggleControl,
} = wpponents;

Unless they've changed anything else about the component, the location you're importing it from should be the only update needed.

Be careful with the first letter of 'serverSideRender', lower case!!

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论