Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

259
Views
Attribute is not saved in Gutenberg

I am trying to create a custom block for Wordpress Gutenberg.

I have the following attributes:

    "icon": {
      "type": "object",
      "source": "html",
      "selector": ".jig_defbox-icon"
    },
    "color": {
      "type": "string",
      "default": "#919191"
    }

In my EditJS I try to store values from a color-picker and a radiogroup into these attributes.

const [ toggled, setToggled ] = useState( );
return(
    <InspectorControls>
        <ColorPalette
            colors={[
                {name: 'lightgray', color: '#d8d8d8'},
                {name: 'darkgray', color: '#919191'},
                {name: 'red', color: '#dc1a22'}
            ]}
            disableCustomColors={true}
            onChange={(value) => {props.setAttributes({color: value});}}
            value={props.attributes.color}
            clearable={false}
        />
        <RadioGroup
            onChange={ (value) => {setToggled(value);props.setAttributes({icon: value});} }
            checked={props.attributes.icon}     
        >
            {
            str_array.map( 
                item => ( <Radio value={item}><Icon icon={icon_getter(item);}/></Radio>)
            )
            }
        </RadioGroup>
</InspectorControls>
)

In my SaveJS I try to render my markup according to these attributes:

const {attributes} = props
<div style={{"fill": attributes.color}}>
    <Icon icon={icon_getter(attributes.icon)}/>
</div>

The goal is to render an svg-icon according to the selection on my radiogroup.

Issue 1: Every new edit-session in backend, the selection of my radiogroup is gone, even with useState (tried without useState first)

Issue 2: Every new edit-session, a console error is logged, that says that the post-body markup does not match the markup returned by the save function, because the save-markup does not contain the icon attribute content

As far as I was able to enclose the problem, the icon attribute is not correctly saved. I tried to save the "key" for the icon as a string and object. If I log the value in the save function, it is empty, while the selected color works as expected, both in frontend and backend.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I was able to fix it via rethinking my concept of fetching the icon.

Apparently, Gutenberg tried to store the code of the icon into the database, but not the key. When loading the backend editor, the icon_getter function received a null value, therefore, the difference between post body and save function code.

I changed my editJS:

<RadioGroup
    onChange={ (value) => {props.setAttributes({icon: value});} }
    checked={props.attributes.icon}     
>
    {
       my_icons_as_list.map( 
           item => ( <Radio value={item}><Icon icon={my_icons[props.attributes.icon];}/></Radio>)
       )
    }
</RadioGroup>

and my saveJS:

<Icon icon={ my_icons[ attributes.icon ] } />
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!