admin管理员组文章数量:1130349
I saw many examples on how to pass props from a block component to another component but they all use ES5 syntax.
In ES6 the edit function when registering a block looks like this:
edit( { attributes, className, setAttributes } ) {
const { title, url, image, content } = attributes;
...etc
I want to pass the attributes and setAttributes to a component. Would this syntax be correct?
<MyComponent { ...{setAttributes, ...attributes } } />
Then in my component would I access them like so: ?
class MyComponent extends Component {
constructor( props ) {
super( ...arguments )
}
render() {
const { title, url, image, content } = this.props.attributes;
const { setAttributes } = this.props;
...etc
There are many ways to do it and not sure which is recommended.
I saw many examples on how to pass props from a block component to another component but they all use ES5 syntax.
In ES6 the edit function when registering a block looks like this:
edit( { attributes, className, setAttributes } ) {
const { title, url, image, content } = attributes;
...etc
I want to pass the attributes and setAttributes to a component. Would this syntax be correct?
<MyComponent { ...{setAttributes, ...attributes } } />
Then in my component would I access them like so: ?
class MyComponent extends Component {
constructor( props ) {
super( ...arguments )
}
render() {
const { title, url, image, content } = this.props.attributes;
const { setAttributes } = this.props;
...etc
There are many ways to do it and not sure which is recommended.
Share Improve this question asked Jan 4, 2019 at 16:07 at least three charactersat least three characters 33712 silver badges28 bronze badges 1- It is not related – at least three characters Commented Jan 4, 2019 at 18:02
1 Answer
Reset to default 3This is a react related issue, so I suggest you take a look at Components and Props.
In this case the edit function passes an argument, which is an object, and we can call props:
registerBlockType("my-plugin/my-block", {
//...
edit: props => {
return <MyComponent {...props} />;
}
});
This way of spreading would be the same as:
<MyComponent attributes={props.attributes} setAttributes={props.setAttributes} />
with all the properties the props object has.
Then inside your component you may access the properties as you are doing in the question:
class MyComponent extends Component {
render() {
const { attributes, setAttributes } = this.props;
const { title, url, image, content } = attributes;
//...etc
Note:
After playing around with blocks and filters I came to the conclusion that it is better to leave the root element of both edit and save functions with an html element rather than a component (and then add the components inside it). This is because Gutenberg changes this root element through filters (for example it adds the necessary classes, as the block name, and allows filters to pass attributes to it), and if you are using your own component you would have to do this by yourself. You can check the columns block to see the differences in the edit and save functions.
registerBlockType("my-plugin/my-block", {
//...
edit: props => {
return (
<div className={props.className}>
<MyComponent {...props} />
</div>
);
},
save: props => {
return (
<div>
<MyComponent {...props} />
</div>
);
},
});
I saw many examples on how to pass props from a block component to another component but they all use ES5 syntax.
In ES6 the edit function when registering a block looks like this:
edit( { attributes, className, setAttributes } ) {
const { title, url, image, content } = attributes;
...etc
I want to pass the attributes and setAttributes to a component. Would this syntax be correct?
<MyComponent { ...{setAttributes, ...attributes } } />
Then in my component would I access them like so: ?
class MyComponent extends Component {
constructor( props ) {
super( ...arguments )
}
render() {
const { title, url, image, content } = this.props.attributes;
const { setAttributes } = this.props;
...etc
There are many ways to do it and not sure which is recommended.
I saw many examples on how to pass props from a block component to another component but they all use ES5 syntax.
In ES6 the edit function when registering a block looks like this:
edit( { attributes, className, setAttributes } ) {
const { title, url, image, content } = attributes;
...etc
I want to pass the attributes and setAttributes to a component. Would this syntax be correct?
<MyComponent { ...{setAttributes, ...attributes } } />
Then in my component would I access them like so: ?
class MyComponent extends Component {
constructor( props ) {
super( ...arguments )
}
render() {
const { title, url, image, content } = this.props.attributes;
const { setAttributes } = this.props;
...etc
There are many ways to do it and not sure which is recommended.
Share Improve this question asked Jan 4, 2019 at 16:07 at least three charactersat least three characters 33712 silver badges28 bronze badges 1- It is not related – at least three characters Commented Jan 4, 2019 at 18:02
1 Answer
Reset to default 3This is a react related issue, so I suggest you take a look at Components and Props.
In this case the edit function passes an argument, which is an object, and we can call props:
registerBlockType("my-plugin/my-block", {
//...
edit: props => {
return <MyComponent {...props} />;
}
});
This way of spreading would be the same as:
<MyComponent attributes={props.attributes} setAttributes={props.setAttributes} />
with all the properties the props object has.
Then inside your component you may access the properties as you are doing in the question:
class MyComponent extends Component {
render() {
const { attributes, setAttributes } = this.props;
const { title, url, image, content } = attributes;
//...etc
Note:
After playing around with blocks and filters I came to the conclusion that it is better to leave the root element of both edit and save functions with an html element rather than a component (and then add the components inside it). This is because Gutenberg changes this root element through filters (for example it adds the necessary classes, as the block name, and allows filters to pass attributes to it), and if you are using your own component you would have to do this by yourself. You can check the columns block to see the differences in the edit and save functions.
registerBlockType("my-plugin/my-block", {
//...
edit: props => {
return (
<div className={props.className}>
<MyComponent {...props} />
</div>
);
},
save: props => {
return (
<div>
<MyComponent {...props} />
</div>
);
},
});
本文标签: javascriptGutenberg passing block attributes to component in ES6ESNext
版权声明:本文标题:javascript - Gutenberg passing block attributes to component in ES6ESNext 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749043874a2307559.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论