Finally, 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>
Related Posts:
- Plugin Sidebar is not Saving Meta Attribute to Post/Page after “Update”
- why is apiFetch throwing Unhandled Promise Rejection: TypeError: Object is not a function
- REST API error in block editor for custom templates
- check if Gutenberg is currently in use
- How to use PanelColorSettings in custom Gutenberg block?
- Adding pre-publish checks with Gutenberg
- Managing two editable fields in gutenberg
- How to make repeated component/block in Gutenberg
- How to save block attributes when the output doesn’t change
- Gutenberg ServerSideRender is deprecated, how to work with new wp.serverSideRender component?
- How to get data from WordPress $wpdb into React Gutenberg Blocks Frontend?
- lodash dependency in a Gutenberg plugin
- How to: Rest endpoint returning empty object
- Custom endpoint that returns an xml
- creating elements/innerblocks via rangecontrol
- What’s the Right Way to get and save remote data for a Gutenberg block?
- WP rest api v2 posts filter by ‘search’ & ‘category_name’ not working
- Error : “Updating failed: The response is not a valid JSON response” with custom shortcode
- Using the component outside the editor. select(‘core’) is null
- Gutenberg is there a way to know if the current block is reusable?
- How to disable “Transform to” option in Gutenberg custom blocks
- How to allow core Gutenberg blocks selection only when you are inside a custom inner block
- What method to use to call a REST API?
- Saving data from block editor to the database
- Make a list with header and subtext in Gutenberg blocks
- How insert negative values in Gutenberg box-control
- Initial help with official “create a block” tutorial
- WP REST API V2 – Add user data to response
- Are block templates incompatible with serialize_blocks?
- How do I dynamically render an InnerBlock inside a dynamic block?
- ServerSideRender and Media Object: attributes passing image data object to php renderer even though it’s not set
- Gutenberg Block showing invalid content on edit
- Gutenberg blocks not getting styled on back end
- How to get results of subcategories also?(WP Rest Api v2)
- Run code once when block is created
- Best way to save custom css for an block
- Amending REST API function without deactivate/activate plugin every time changes is made
- How can I listen to events in Gutenberg block?
- It’s possible to passing a ref to FormTokenField from parent component using forwardRef?
- Getting meta in editor plugin, and event triggering issue
- WP Gutenberg Blocks – How to limit first/root blocks
- JSX in WordPress Plugin Development
- ResizableBox with RangeControl not working
- Gutenberg: import dependency or assign from global variable?
- Secure REST with nonce works only when not logged
- How to do POST request with WP REST API v2 to custom table?
- Gutenberg table block with Bootstrap .table class
- How to add a gradient component to a custom block
- Custom Rest Endpoint: Post/Put/Patch not working
- Catching Gutenberg sidebar switch event
- Can’t retrieve body params from PUT endpoint using formdata
- Use npm and wp-env to make production bundle
- How to add template colors to custom block options in WordPress Gutenberg editor sidebar?
- Do I need to create an endpoint?
- Gutenberg blocks error: Each child in a list should have a unique “key” prop
- How to properly package a block plugin
- Setting global block attribute value
- Mock a dependency of a WP_REST_Controller class
- Modify custom block plugin without losing content
- Custom Block Not Shown in Block Inserter
- How to override supports of innerBlocks?
- Allow REST API Endpoint to specific user and hide from public
- How to do admin ajax request in a plugin for rest api
- When setting styles to an extended block that was saved as a pattern, it doesn’t remove the style that was there
- Query String Filtering API
- extraprops override existing props
- Gutenberg block breaks when showing new data
- Get Custom taxonomy parent with wordpress REST API
- register_meta not showing custom post type metabox data in rest api
- Is “document loaded” different on admin side than public side?
- How to re-render inspector controls?
- Securing custom rest API endpoints with public access from PWA
- WP_Query not ordering correctly
- 403 error rest_’cookie_invalid_nonce’ on API Request
- Get value of custom checkbox from Gutenberg sidebar in plugin
- Where to save Gutenberg plugin data?
- WC REST API Error Handling
- Gutenberg component in a plugin admin
- Add custom html classes to gutenberg wrapper
- How to create save function for custom Gutenberg style block?
- React in plugins: REST endpoints vs. regular functions
- WP API users per page
- Best place to keep files that are called by cron jobs?
- Is it safe to use PUT and DELETE requests
- Gutenberg and custom blocks messed up pagespeed score. What did I wrong?
- Gutenberg text field validation
- Undo operation not working in Rich text [Gutenberg]
- How to update WordPress Plugins in your own maintance application?
- Render raw html in Gutenberg block
- Extend Plugin that creates an endpoint for GET request to also accepts POST requests
- Help interpreting @wordpress/create-block-tutorial-template usage error
- How can create a custom plugin to call my webapi after any registration or membership plugin functionality
- Rest Api WordPress
- How can I add a custom button to the post editor?
- Block Development: hamburger module throwing error in save function
- Adding a navigation with wp_nav_menu() to a custom block in the site editor
- Why isn’t custom sidebar panel not showing up in the Gutenberg Editor?
- Reinitiate Gutenburg’s blocks using javascript
- Execute callback after REST API request completes + Response is sent
- Set srcset on a block image