admin管理员组文章数量:1389903
I'm trying to create author info block for posts.
So I created a block and tried to use redux to get author info, but could get only author id:
const applyWithSelect = withSelect(
(select, props) => {
return {
authorId: select('core/editor').getCurrentPostAttribute('author')
}
}
);
Then I tried to use apiFetch to get author info by its id:
const authorApi = apiFetch( { path: `/wp/v2/users/${authorId}` } ).then(data => {console.log(data)});
So it's actually working, I'm getting all author information in my console. But how to get that data values to map through it?
What should I insert inside then()
?
I think I should create an empty array and push that data values inside of it.
But I don't know how, my knowledge is not enough.
Can you help me, please?
I'm trying to create author info block for posts.
So I created a block and tried to use redux to get author info, but could get only author id:
const applyWithSelect = withSelect(
(select, props) => {
return {
authorId: select('core/editor').getCurrentPostAttribute('author')
}
}
);
Then I tried to use apiFetch to get author info by its id:
const authorApi = apiFetch( { path: `/wp/v2/users/${authorId}` } ).then(data => {console.log(data)});
So it's actually working, I'm getting all author information in my console. But how to get that data values to map through it?
What should I insert inside then()
?
I think I should create an empty array and push that data values inside of it.
But I don't know how, my knowledge is not enough.
Can you help me, please?
Share Improve this question asked Apr 8, 2020 at 16:10 AslanAslan 557 bronze badges1 Answer
Reset to default 1Finally, I found out how to do it. Just call fetchApi in componentDidMount and then create a new state in your component and add the response to this state when the request is done:
constructor(props) {
super(props);
this.state = {
data: {},
};
}
componentDidMount() {
apiFetch( { path: '/wp/v2/users/1' } )
.then(data => this.setState({ data }));
}
then in render:
const { data } = this.state;
finally in return for example avatar of author:
<div className={className}>
{ data.avatar_url ? (<img src={data.avatar_url['24']} />) : (<></>) }
</div>
本文标签: plugin developmentHow to use apiFetch to get author information in Gutenberg properly
版权声明:本文标题:plugin development - How to use apiFetch to get author information in Gutenberg properly? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744585061a2614148.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论