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 |1 Answer
Reset to default 0I 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
版权声明:本文标题:plugin development - move useState variable from edit.js to save.js. How do I do that? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736661536a1946440.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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