admin管理员组

文章数量:1125579

I want to move the useState variable selectedValue once it's defined in edit.js and import it into save.js. In edit. js the code looks like this


import { __ } from '@wordpress/i18n';
import {
    useBlockProps,
    InspectorControls,
    PanelColorSettings
} from '@wordpress/block-editor';
import {
    PanelBody,
    PanelRow
} from '@wordpress/components';
import { useState } from '@wordpress/element';
import './editor.scss';
import {options, catagories}from './app';
export default function Edit({ attributes, setAttributes }) {
    const blockProps = useBlockProps();
    const { backgroundColor, textColor } = attributes;

    const updateGroupId = (val) => {
        setAttributes({ GroupId: parseInt(val) });
    }
    const onChangeBackgroundColor = (BackgroundColor) => {
        setAttributes({ backgroundColor: BackgroundColor })
    }
    const onChangeTextColor = (TextColor) => {
        setAttributes({ textColor: TextColor })
    }   
    const stringArray = catagories.map(JSON.stringify);
    const uniqueStringArray = new Set(stringArray);
    const uniqueArray = Array.from(uniqueStringArray, JSON.parse);
    const [selectedValue, setSelectedValue] = useState('vtalk');
    
    const handleSelectChange = (event) => {
          setSelectedValue(event.target.value);
          console.log(event.target.value);
        }
    
    const mapFile = options.filter((m, idx) => idx < 5 && m.catagory == selectedValue).map((m) => { return (<div class="blog-stamp"><img src={m.image} width="200" height="200" /><h3 class="stamp-title">{m.label}</h3><p class="stamp-excerpt">{m.content}</p><p class="button-parent"><a href={m.link} class="sfs-button" style={{ backgroundColor: backgroundColor, color: textColor }}>Read more</a></p></div>) });
    return (
        <div {...blockProps}>
            <InspectorControls>
                <PanelColorSettings
                    title={__('Button Color settings', 'sfs-block')}
                    initialOpen={false}
                    colorSettings={[
                        {
                            value: textColor,
                            onChange: onChangeTextColor,
                            label: __('Button Text color', 'sfs-block')
                        },
                        {
                            value: backgroundColor,
                            onChange: onChangeBackgroundColor,
                            label: __('Button Background color', 'sfs-block')
                        }
                    ]}
                />
                <PanelBody
                    title={__('catagory filter', 'sfs-block')}
                    initialOpen={true}
                >
                    <PanelRow>
                        <fieldset>
                        <select value={selectedValue} onChange={handleSelectChange}>
                                {uniqueArray.map((cat) => {
                                    return <option value={cat.catagory}>{cat.catagory}</option>
                                })}
                            </select>
                        </fieldset>
                    </PanelRow>
                </PanelBody>
            </InspectorControls>
            {console.log(uniqueArray)}
            <p class="blog-stamp-parent">{mapFile}</p>
            </div>

    );
} 

To explain what is going on. The user selects a value from the field set. That is set as the select value. I know want to import that value into save.js. But can't figure out how.

Here is what save.js looks at the moment

import { useBlockProps } from '@wordpress/block-editor';
import {options}from './app';

export default function save({attributes}) {
    const { backgroundColor, textColor } = attributes;
    const mapFile = options.filter((m, idx) => idx < 5 && m.catagory == selectedValue).map((m) => { return (<div class="blog-stamp"><img src={m.image} width="200" height="200" /><h3 class="stamp-title">{m.label}</h3><p class="stamp-excerpt">{m.content}</p><p class="button-parent"><a href={m.link} class="sfs-button" style={{ backgroundColor: backgroundColor, color: textColor }}>Read more</a></p></div>) });

    return (
        <div{ ...useBlockProps.save() }>
        <p class="blog-stamp-parent">{mapFile}</p>
        </div>
    );
}

As you can see in const mapFile you render selectedValue in save.js. Just need help to get it there. Thanks in advance

I want to move the useState variable selectedValue once it's defined in edit.js and import it into save.js. In edit. js the code looks like this


import { __ } from '@wordpress/i18n';
import {
    useBlockProps,
    InspectorControls,
    PanelColorSettings
} from '@wordpress/block-editor';
import {
    PanelBody,
    PanelRow
} from '@wordpress/components';
import { useState } from '@wordpress/element';
import './editor.scss';
import {options, catagories}from './app';
export default function Edit({ attributes, setAttributes }) {
    const blockProps = useBlockProps();
    const { backgroundColor, textColor } = attributes;

    const updateGroupId = (val) => {
        setAttributes({ GroupId: parseInt(val) });
    }
    const onChangeBackgroundColor = (BackgroundColor) => {
        setAttributes({ backgroundColor: BackgroundColor })
    }
    const onChangeTextColor = (TextColor) => {
        setAttributes({ textColor: TextColor })
    }   
    const stringArray = catagories.map(JSON.stringify);
    const uniqueStringArray = new Set(stringArray);
    const uniqueArray = Array.from(uniqueStringArray, JSON.parse);
    const [selectedValue, setSelectedValue] = useState('vtalk');
    
    const handleSelectChange = (event) => {
          setSelectedValue(event.target.value);
          console.log(event.target.value);
        }
    
    const mapFile = options.filter((m, idx) => idx < 5 && m.catagory == selectedValue).map((m) => { return (<div class="blog-stamp"><img src={m.image} width="200" height="200" /><h3 class="stamp-title">{m.label}</h3><p class="stamp-excerpt">{m.content}</p><p class="button-parent"><a href={m.link} class="sfs-button" style={{ backgroundColor: backgroundColor, color: textColor }}>Read more</a></p></div>) });
    return (
        <div {...blockProps}>
            <InspectorControls>
                <PanelColorSettings
                    title={__('Button Color settings', 'sfs-block')}
                    initialOpen={false}
                    colorSettings={[
                        {
                            value: textColor,
                            onChange: onChangeTextColor,
                            label: __('Button Text color', 'sfs-block')
                        },
                        {
                            value: backgroundColor,
                            onChange: onChangeBackgroundColor,
                            label: __('Button Background color', 'sfs-block')
                        }
                    ]}
                />
                <PanelBody
                    title={__('catagory filter', 'sfs-block')}
                    initialOpen={true}
                >
                    <PanelRow>
                        <fieldset>
                        <select value={selectedValue} onChange={handleSelectChange}>
                                {uniqueArray.map((cat) => {
                                    return <option value={cat.catagory}>{cat.catagory}</option>
                                })}
                            </select>
                        </fieldset>
                    </PanelRow>
                </PanelBody>
            </InspectorControls>
            {console.log(uniqueArray)}
            <p class="blog-stamp-parent">{mapFile}</p>
            </div>

    );
} 

To explain what is going on. The user selects a value from the field set. That is set as the select value. I know want to import that value into save.js. But can't figure out how.

Here is what save.js looks at the moment

import { useBlockProps } from '@wordpress/block-editor';
import {options}from './app';

export default function save({attributes}) {
    const { backgroundColor, textColor } = attributes;
    const mapFile = options.filter((m, idx) => idx < 5 && m.catagory == selectedValue).map((m) => { return (<div class="blog-stamp"><img src={m.image} width="200" height="200" /><h3 class="stamp-title">{m.label}</h3><p class="stamp-excerpt">{m.content}</p><p class="button-parent"><a href={m.link} class="sfs-button" style={{ backgroundColor: backgroundColor, color: textColor }}>Read more</a></p></div>) });

    return (
        <div{ ...useBlockProps.save() }>
        <p class="blog-stamp-parent">{mapFile}</p>
        </div>
    );
}

As you can see in const mapFile you render selectedValue in save.js. Just need help to get it there. Thanks in advance

Share Improve this question edited Feb 6, 2024 at 13:39 Dave B asked Feb 6, 2024 at 10:53 Dave BDave B 255 bronze badges 5
  • It you want to save state to a block you need to set it to an attribute value. selectedValue should be an attribute, not state. – Jacob Peattie Commented Feb 6, 2024 at 11:05
  • Sorry. I don't understand. It a variable set by state. How can it be an attribute? Can you explain. @JacobPeattie – Dave B Commented Feb 6, 2024 at 11:29
  • Well what are you doing for backgroundColor? – Jacob Peattie Commented Feb 6, 2024 at 11:59
  • @DaveB when you register your block you also defined all the attributes that could be stored on it. If you didn't do this then you stored no attributes. You should add attributes, they'll be passed to your edit and save components, and you can set them. backgroundColor, textColor are attributes. This is all documented in the block editor handbook on the WP developer site – Tom J Nowell Commented Feb 6, 2024 at 12:44
  • @JacobPeattie it's possible that's coming from core as a side effect of declaring the block supports background colours – Tom J Nowell Commented Feb 6, 2024 at 12:44
Add a comment  | 

1 Answer 1

Reset to default 0

I want to move the useState variable selectedValue once it's defined in edit.js and import it into save.js. In edit. js the code looks like this

This is impossible and not how save components work.

A save component takes a set of block attributes, and generates static HTML to be saved in the database. It's also used to validate block markup. The save component should always return the same HTML when given the same block attributes.

You cannot:

  • use state
  • make remote API requests
  • use lifecycle hooks such as useEffect etc
  • use callbacks
  • use interactive UI

If you want to move use state in the edit component, you have to save it in a block attribute.

A Major Mistake You Haven't Noticed

const [selectedValue, setSelectedValue] = useState('vtalk');

Storing the vtalk value via useState might make sense, but the edit component is not persistent, it can and will be destroyed and recreated arbitrarily, in fact you should expect this!.

As this happens, it's possible the edit components internal state might not survive, erasing the value, and it won't persist between page refreshes either.

The solution to this is also to store it in a block attribute. Any local state should be considered temporary and purely used for building the UI.

TLDR: There is no reason to use state in any of the code you have provided, get/set an attribute instead.

本文标签: plugin developmentmove useState variable from editjs to savejs How do I do that