Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

262
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda