• Guido

    (@guido07111975)


    Hi,

    I’m working on my first block.
    It’s not clear to me whether I should add a key or not, when creating an element in React. Example:

    React.createElement('a', {
    	key: "my-key",
    	href: "my-url",
    	target: "_blank"
    }, 'My label')

    I do find info online about using key when creating a select field (= multiple option values). That I understand.
    But also in case of a simple input field? Or when static content is returned such as my example above?

    Guido

Viewing 4 replies - 1 through 4 (of 4 total)
  • threadi

    (@threadi)

    I add a key to each element, otherwise this leads to a warning when loading in Gutenberg.

    Thread Starter Guido

    (@guido07111975)

    Hi,

    I have debugging on but no warnings here, when omitting keys.. Or do you mean the Gutenberg plugin instead of block editor?

    Guido

    threadi

    (@threadi)

    No, I mean the block editor. I only use the Gutenberg plugin during development to see if what I’ve written can be used in the future.

    If your code works without the key, then continue to use it as it is.

    Thread Starter Guido

    (@guido07111975)

    In the WP tutorials that I’ve found, such as this one, key is not mentioned. But I notice that block elements are created in another way than with function createElement(). Guess WP uses its own function and key is added automatically.

    Guido

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Blocks – createElement – add key or not’ is closed to new replies.