Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

261
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda