wordpress - 如何使用“withSelect”更新动态 Gutenberg 块中的 REST-API 调用
问题描述
我创建了一个相当复杂的 Gutenebrg Block
我已经硬编码了帖子/项目 ID,现在我正在努力如何实现更新功能。我使用两个不同的 API 路由,并希望通过在字段中输入 ID 让用户可以更改块内容。
将属性传递到:
withSelect(select => {
return {
posts: select('core').getEntityRecords('/wp/v2', 'projects', {include: 2962}),
posts1: select('core').getEntityRecords('/wp/v2', 'posts', {include: 3432})
};
})
下面的大部分代码......作为一个古腾堡菜鸟,这对我来说相当棘手......
/**
* Block dependencies
*/
import './style.scss';
/**
* Internal block libraries
*/
const { __ } = wp.i18n;
const { registerBlockType } = wp.blocks;
const { Spinner } = wp.components;
const { withSelect, useSelect } = wp.data;
const {
InspectorControls,
RichText,
MediaUpload,
URLInputButton
} = wp.blockEditor;
var dispatch = wp.data.dispatch;
dispatch( 'core' ).addEntities( [
{
name: 'projects', // route name
kind: '/wp/v2', // namespace
baseURL: '/wp/v2/projects/' // API path without /wp-json
},
{
name: 'posts', // route name
kind: '/wp/v2', // namespace
baseURL: '/wp/v2/posts/' // API path without /wp-json
}
]);
registerBlockType(
'jsforwpblocks/dynamic',
{
title: __( 'Example - Dynamic Block', 'jsforwpblocks'),
description: __( 'A look at how to build a basic dynamic block.', 'jsforwpblocks'),
icon:'shield',
category: 'widgets',
attributes: {
projektID: {
type: 'string',
},
},
edit:
withSelect( select => {
return {
posts: select( 'core' ).getEntityRecords( '/wp/v2', 'projects',{include: 2962}),
posts1: select( 'core' ).getEntityRecords( '/wp/v2', 'posts',{include: 3432})
};
} )
( ( { props, posts, posts1, className, isSelected, setAttributes,attributes } ) => {
if ( ! posts ) {
return (
<p className={className} >
<Spinner />
{ __( 'Loading Posts', 'jsforwpblocks' ) }
</p>
);
}
if ( 0 === posts.length ) {
return <p>{ __( 'No Posts', 'jsforwpblocks' ) }</p>;
}
function changeprojektid(projektID) {
// using some nice js features instead of typing
// { heading: heading }
setAttributes({ projektID });
withSelect( select => {
let query = 'include: 2962'
return {
posts: select( 'core' ).getEntityRecords( '/wp/v2', 'projects', {include: 2962})
};
} )
}
return (
<div>
<div className="copy">
<RichText
className="heading"
tagName="h2"
placeholder="Enter your ID"
value={attributes.projektID}
onChange={changeprojektid}
/>
</div>
<ul className={ className }>
{ posts.map( post => {
return (
<li>
<a className={ className } href={ post.link }>
{ post.title.rendered }
<img src={ post.projimage1.guid } />
</a>
</li>
);
}) }
</ul>
<ul className={ className }>
{ posts1.map( post => {
return (
<li>
<a className={ className } href={ post.link }>
{ post.title.rendered }
</a>
</li>
);
}) }
</ul>
</div>
);
} )
// end withAPIData
, // end edit
解决方案
推荐阅读
- sql - 如何使用 BETWEEN 和 OR 使用 SQL 案例表达式?
- javascript - 在路线路径的开头添加子路线(角度)
- swift - 为什么我有这个致命错误在隐式展开可选值时意外发现 nil
- python - Python - 分组和分配异常规则
- json - 配置Visual Studio Code任务依次执行多个shell命令
- coq - 如何将引理应用于 2 个假设
- python - 提炼
用 Scrapy - android - 使用 MediaStore 存储音频文件在设置 TITLE、ARTIST、COMPOSER 等时失败
- google-cloud-platform - 我想在执行删除操作时降低 bigquery 中的查询成本
- mysql-workbench - 在 MySQLWorkBench GUI 中看不到表