admin管理员组文章数量:1122832
I'm creating a block and using rich text to save some text this is my edit function.
edit: (props) => {
const { attributes: { content, backgroundImage }, setAttributes } = props;
const onImageSelect = (imageObject) => {
setAttributes({ backgroundImage: imageObject.sizes.full.url });
}
const onChangeContent = (newContent) => {
setAttributes({ content: newContent });
};
console.log(content);
return (
<div className={props.className}>
<MediaUpload
onSelect={onImageSelect}
type="image"
value={backgroundImage}
render={({ open }) => (
<button onClick={open}>
Upload Image!
</button>
)}
/>
<img src={backgroundImage} alt="eherh" />
<div className={'caption'}>
<RichText
tagName={"p"}
className={'imgCtaContent'}
onChange={onChangeContent}
value={content}
placeholder="Enter text..."
/>
</div>
</div>
);
},
and this is my save function: -
save: (props) => {
return (
<div className={props.className}>
<RichText.Content tagName={"p"} value={props.attributes.content} />
</div>
);
},
I can see when I type into the block when I'm editing it that the console.log(content) displays the content but when I save the page the text isn't saved.
Where am I going wrong?
I'm creating a block and using rich text to save some text this is my edit function.
edit: (props) => {
const { attributes: { content, backgroundImage }, setAttributes } = props;
const onImageSelect = (imageObject) => {
setAttributes({ backgroundImage: imageObject.sizes.full.url });
}
const onChangeContent = (newContent) => {
setAttributes({ content: newContent });
};
console.log(content);
return (
<div className={props.className}>
<MediaUpload
onSelect={onImageSelect}
type="image"
value={backgroundImage}
render={({ open }) => (
<button onClick={open}>
Upload Image!
</button>
)}
/>
<img src={backgroundImage} alt="eherh" />
<div className={'caption'}>
<RichText
tagName={"p"}
className={'imgCtaContent'}
onChange={onChangeContent}
value={content}
placeholder="Enter text..."
/>
</div>
</div>
);
},
and this is my save function: -
save: (props) => {
return (
<div className={props.className}>
<RichText.Content tagName={"p"} value={props.attributes.content} />
</div>
);
},
I can see when I type into the block when I'm editing it that the console.log(content) displays the content but when I save the page the text isn't saved.
Where am I going wrong?
Share Improve this question asked Feb 11, 2020 at 22:40 AstridAstrid 1231 silver badge12 bronze badges2 Answers
Reset to default 1I had a similar bug and solved it by just declaring the "type" (not "source" and "selector") when registering the Block Type.
attributes: {
content: {
type: 'string',
// source: 'html',
// selector: 'h2',
},
},
Couple of things to try - remove some of the curly braces { } because those are only needed when you're referring to variables, and try just directly calling onChange:
<div className={'caption'}>
<RichText
tagName="p"
className='imgCtaContent'
onChange={ ( content ) => { setAttributes( { content } ) } }
value={content}
placeholder="Enter text..."
/>
</div>
Also in save()
, setting a value on RichText.content isn't needed and may be confusing things. Just use
return (
<div className={props.className}>
<RichText.Content />
</div>
);
本文标签: pluginsGutenberg block can39t save richtext
版权声明:本文标题:plugins - Gutenberg block can't save richtext 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736300417a1930807.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论