Proper way to use useSelect

时间:2021-12-11 作者:user8463989

我正在使用useSelect获取可用的图像大小,然后我想检查图像是否实际具有这些大小,因为可能添加了一些自定义图像大小。

我所做的工作正在进行,但我担心实现不正确,因为这是我第一次使用useSelect。

import { compose } from "@wordpress/compose";
import { useBlockProps } from "@wordpress/block-editor";
import { isBlobURL } from "@wordpress/blob";
import {
    MediaPlaceholder,
    BlockControls,
    MediaUpload,
    MediaUploadCheck,
    InspectorControls,
} from "@wordpress/block-editor";
import {
    Toolbar,
    IconButton,
    PanelBody,
    SelectControl,
} from "@wordpress/components";
const { useSelect } = wp.data;

export function Edit(props) {


    const blockProps = useBlockProps();
    const {
        attributes: { id, alt, url },
        setAttributes,
    } = props;

    
    const imageSizes = useSelect(
        (select) => {
            return {
                imageSizes: select("core/editor").getEditorSettings().imageSizes,
                image: id ? select("core").getMedia(id) : null,
            };
        },
        [id]
    );

    const onSelectImage = ({ id, url, alt }) => {
        setAttributes({ id, url, alt });
    };

    function getImageSizes() {
        const { image } = imageSizes;
        if (!image) return [];
        let options = [];
        const sizes = image.media_details.sizes;
        for (const key in sizes) {
            const size = sizes[key];
            const imageSize = imageSizes.imageSizes.find((size) => size.slug === key);
            if (imageSize) {
                options.push({
                    label: imageSize.name,
                    value: size.source_url,
                });
            }
        }
        return options;
    }

    return (
        <>
            {url && !isBlobURL(url) && (
                <InspectorControls>
                    <PanelBody title="Image Settings">
                        {id && (
                            <SelectControl
                                label="Image size"
                                options={getImageSizes()}
                                onChange={(value) => console.log(value)}
                            />
                        )}
                    </PanelBody>
                </InspectorControls>
            )}
            <div {...blockProps}>
                <div>
                    {url ? (
                        <>
                            <img src={url} alt={alt} />
                            {isBlobURL(url) && <Spinner />}
                        </>
                    ) : (
                        <MediaPlaceholder
                            icon="format-image"
                            onSelect={onSelectImage}
                            onError={onUploadError}
                            accept="image/*"
                            allowedTypes={["image"]}
                        />
                    )}

                </div>
            </div>
        </>
    );
}

export default Edit;

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

我担心实施不正确

如果你只是想知道你是否正确使用了useSelect, 那么,是的,您已经返回了-我还将返回与回调返回的对象相同的对象。

但是const imageSizes = useSelect( ... ), 我会的const { image, imageSizes } = useSelect( ... ), i、 e.打开属性,然后我就不需要const { image } = imageSizes; 我也会用imageSizes 而不是imageSizes.imageSizes.

但这只是我个人的偏好,其中一个原因是,解包会使代码更少。

相关推荐