首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Gutenberg中,现在已经不再推荐withAPIData了,我如何才能对自定义端点执行异步调用?

在Gutenberg中,现在已经不再推荐withAPIData了,我如何才能对自定义端点执行异步调用?
EN

WordPress Development用户
提问于 2018-08-14 15:25:25
回答 2查看 1.9K关注 0票数 8

我在我的插件中使用了这个插件,一切都很好:

代码语言:javascript
复制
edit: withAPIData( props => {
            return {
                roles: '/matt-watson/secure-blocks/v1/user-roles/'
            };
        } )( props => {

……

然而,这里的指南表明,这将不再被支持:https://github.com/WordPress/gutenberg/issues/7390#issuecomment-398667802

因此,我设法将其应用到解决方案中:

代码语言:javascript
复制
const DEFAULT_STATE = {
userRoles: {},
};

const actions = {
    setUserRoles( userRoles ) {
        return {
            type: 'SET_USER_ROLES',
            userRoles,
        };
    },

    fetchFromAPI( path ) {
        return {
            type: 'FETCH_FROM_API',
            path,
        };
    },
};

registerStore( 'matt-watson/secure-block', {
    reducer( state = DEFAULT_STATE, action ) {

        switch ( action.type ) {
            case 'SET_USER_ROLES':
                return {
                    ...state,
                    userRoles: action.userRoles,
                };
        }

        return state;
    },

    actions,

    selectors: {
        getUserRoles( state ) {
            const { userRoles } = state;
            return userRoles;
        },
    },

    controls: {
        FETCH_FROM_API( action ) {
            return apiFetch( { path: action.path } );
        },
    },

    resolvers: {
        * getUserRoles( state ) {
            const path = '/matt-watson/secure-blocks/v1/user-roles/';
            const userRoles = yield actions.fetchFromAPI( path );
            return actions.setUserRoles( userRoles );
        },
    }

    ,
    } );

……

代码语言:javascript
复制
edit: withSelect( ( select ) => {
                                return {
                                    roles: select('matt-watson/secure-block').getUserRoles(),
                                };
                            } )( props => {

……

现在的问题是,“控件”部分永远不会触发。在文档中,它说这是一个可选择的组件,我需要通过use命令:https://github.com/WordPress/gutenberg/tree/master/packages/data#controls来启用它。

当我尝试这样做时,use是未定义的。

所以我的问题是这个。当withAPIData以一种非常快速和简单的方式完成我所需要的一切时,我是否可以使用另一种“插入”代码,或者我是否需要使用我正在注册的商店?如果是的话,我做错了什么?

任何帮助都很感激。谢谢。

EN

回答 2

WordPress Development用户

回答已采纳

发布于 2018-12-10 14:32:44

值得注意的是,上面的答案已经过时了。数据存储现在应该如下所示:

代码语言:javascript
复制
const actions = {
    setUserRoles( userRoles ) {
        return {
            type: 'SET_USER_ROLES',
            userRoles,
        };
    },
    receiveUserRoles( path ) {
        return {
            type: 'RECEIVE_USER_ROLES',
            path,
        };
    },
};

const store = registerStore( 'matt-watson/secure-block', {
    reducer( state = { userRoles: {} }, action ) {

        switch ( action.type ) {
            case 'SET_USER_ROLES':
                return {
                    ...state,
                    userRoles: action.userRoles,
                };
        }

        return state;
    },

    actions,

    selectors: {
        receiveUserRoles( state ) {
            const { userRoles } = state;
            return userRoles;
        },
    },

    controls: {
        RECEIVE_USER_ROLES( action ) {
            return apiFetch( { path: action.path } );
        },
    },

    resolvers: {
        * receiveUserRoles( state ) {
            const userRoles = yield actions.receiveUserRoles( '/matt-watson/secure-blocks/v1/user-roles/' );
            return actions.setUserRoles( userRoles );
        },
    },
} );

我更新了我的教程:https://mwatson.co.uk/working-with-gutenberg-and-the-wordpress-rest-api/

票数 5
EN

WordPress Development用户

发布于 2018-08-15 19:29:56

我已经破解了!

代码语言:javascript
复制
const actions = {
    setUserRoles( userRoles ) {
        return {
            type: 'SET_USER_ROLES',
            userRoles,
        };
    },

    receiveUserRoles( path ) {
        return {
            type: 'RECEIVE_USER_ROLES',
            path,
        };
    },
};

const store = registerStore( 'matt-watson/secure-block', {
    reducer( state = { userRoles: {} }, action ) {

        switch ( action.type ) {
            case 'SET_USER_ROLES':
                return {
                    ...state,
                    userRoles: action.userRoles,
                };
            case 'RECEIVE_USER_ROLES':
                return action.userRoles;
        }

        return state;
    },

    actions,

    selectors: {
        receiveUserRoles( state ) {
            const { userRoles } = state;
            return userRoles;
        },
    },

    resolvers: {
        * receiveUserRoles( state ) {
            const userRoles = apiFetch( { path: '/matt-watson/secure-blocks/v1/user-roles/' } )
                .then( userRoles => {
                    return actions.setUserRoles( userRoles );
                } )
            yield userRoles;
        },
    },

} );

这将设置userRoles的状态,并在解析后返回userRoles。

然后,您可以这样访问:

……

代码语言:javascript
复制
edit: withSelect( ( select ) => {
                return {
                    roles: select('matt-watson/secure-block').receiveUserRoles(),
                };
            } )( props => {

……

票数 4
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/311431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档