admin管理员组文章数量:1129399
I'm developing Gutenberg custom block for accodrdions on my website. My code was working with only one content field. When I have added second props it has stop worked and console says:
Cannot read property 'content' of undefined
wp.blocks.registerBlockType('myblock/question-block', {
title: 'Blok Pytan',
icon: 'dashicons-welcome-write-blog',
category: 'common',
attributes: {
header: {type: 'string'},
content: {type: 'string'}
},
edit: function(props, propstwo) {
function updateheader(event) {
props.setAttributes({header: event.target.value})
}
function updatecontent(event) {
propstwo.setAttributes({content: event.target.value})
}
return wp.element.createElement(
"div",
null,
wp.element.createElement(
"h2",
null,
"Nagłówek tekstu"
),
wp.element.createElement("input", { type: "text", value: props.attributes.header, onChange: updateheader }),
),
wp.element.createElement(
"p",
null,
"Rozwijany tekst"
),
wp.element.createElement("input", { type: "text", value: propstwo.attributes.content, onChange: updatecontent })
},
save: function(props, propstwo) {
return wp.element.createElement(
"div",
{className: "accodrion"},
wp.element.createElement(
"h2",
{className: "accodrdion-header"},
props.attributes.header
),
wp.element.createElement(
"p",
{className: "panel"},
propstwo.attributes.content
)
)}
})
I'm developing Gutenberg custom block for accodrdions on my website. My code was working with only one content field. When I have added second props it has stop worked and console says:
Cannot read property 'content' of undefined
wp.blocks.registerBlockType('myblock/question-block', {
title: 'Blok Pytan',
icon: 'dashicons-welcome-write-blog',
category: 'common',
attributes: {
header: {type: 'string'},
content: {type: 'string'}
},
edit: function(props, propstwo) {
function updateheader(event) {
props.setAttributes({header: event.target.value})
}
function updatecontent(event) {
propstwo.setAttributes({content: event.target.value})
}
return wp.element.createElement(
"div",
null,
wp.element.createElement(
"h2",
null,
"Nagłówek tekstu"
),
wp.element.createElement("input", { type: "text", value: props.attributes.header, onChange: updateheader }),
),
wp.element.createElement(
"p",
null,
"Rozwijany tekst"
),
wp.element.createElement("input", { type: "text", value: propstwo.attributes.content, onChange: updatecontent })
},
save: function(props, propstwo) {
return wp.element.createElement(
"div",
{className: "accodrion"},
wp.element.createElement(
"h2",
{className: "accodrdion-header"},
props.attributes.header
),
wp.element.createElement(
"p",
{className: "panel"},
propstwo.attributes.content
)
)}
})
Share
Improve this question
asked Mar 18, 2019 at 15:03
Dom.inDom.in
335 bronze badges
2 Answers
Reset to default 2You don't need propstwo
. It doesn't serve any purpose. There's only one props
argument, and you use it to get access to your block's attributes
. Both header
and content
are attributes, so you access both of them through the same props
argument:
wp.blocks.registerBlockType('myblock/question-block', {
title: 'Blok Pytan',
icon: 'dashicons-welcome-write-blog',
category: 'common',
attributes: {
header: {
type: 'string'
},
content: {
type: 'string'
}
},
edit: function(props) {
function updateheader(event) {
props.setAttributes({
header: event.target.value
})
}
function updatecontent(event) {
props.setAttributes({
content: event.target.value
})
}
return wp.element.createElement(
"div",
null,
wp.element.createElement(
"h2",
null,
"Nagłówek tekstu"
),
wp.element.createElement(
"input",
{
type: "text",
value: props.attributes.header,
onChange: updateheader
}
),
wp.element.createElement(
"p",
null,
"Rozwijany tekst"
),
wp.element.createElement(
"input",
{
type: "text",
value: props.attributes.content,
onChange: updatecontent
}
),
);
},
save: function(props) {
return wp.element.createElement(
"div",
{
className: "accodrion",
},
wp.element.createElement(
"h2", {
className: "accodrdion-header"
},
props.attributes.header
),
wp.element.createElement(
"p", {
className: "panel"
},
props.attributes.content
)
);
}
})
You also hadn't correctly nested the child elements in your edit
function, which I've also corrected above.
@jacob
The last comma on the ) before save needs to be removed, otherwise it's golden:
),<--here
}, save: function(props) {
本文标签: javascriptWP Gutenbergcustom block with two content fields
版权声明:本文标题:javascript - WP Gutenberg - custom block with two content fields 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736744375a1950688.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论