Cannot get dataview to render items
-
I wanted to try out the @wordpress/dataviews package. I imported @wordpress/dataviews/wp as the docs suggested. I ran into a wide variety of React errors. The relatively simple config found in the various docs scattered all over the place led to React errors.
I found that without paginationInfo and specifically providing an infinite scrolling function callback, this would produce errors. I’m just trying to test out basic grid function, so it’s very frustrating having to cycle through so much config where any mistake might lead to it not functioning.
I eventually got the header of the grid showing with the filter and search bar, but none of the data would show up. I’ve shared the code I ended up with below. I tried dozens of variations and read through all the examples available but of course the docs are scattered and they all contradict each either on what the format of some of the config objects should be.
I suspect the issue is that some type of config is required for layouts? By the way I switched to composition mode as a last ditch effort to get anything remotely working, but most of my testing was with controlled mode.
import { registerBlockType } from '@wordpress/blocks';
import { DataViews } from '@wordpress/dataviews/wp';
import { useState } from '@wordpress/element';
function Edit() {
const [view, setView] = useState({
type: 'table',
perPage: 10,
page: 1,
});
// Dummy function for infiniteScrollHandler
const infiniteScrollHandler = () => {};
const data = [
{
id: 1,
title: 'First Post',
date: '2025-12-01T10:00:00',
author: 'Alice',
},
{
id: 2,
title: 'Second Post',
date: '2025-12-01T12:00:00',
author: 'Bob',
},
];
const { __ } = wp.i18n;
function getFormattedDate(date) {
// Simple ISO to locale string for demo; replace with your formatting as needed
return new Date(date).toLocaleString();
}
const fields = [
{
id: 'title',
type: 'text',
label: 'Title',
},
];
return (
<DataViews
data={data}
fields={fields}
view={view}
onChangeView={setView}
paginationInfo={{
totalItems: data.length,
totalPages: 1,
infiniteScrollHandler,
}}
defaultLayouts={{ table: {} }}
>
<h1>{ __( 'Free composition' ) }</h1>
<DataViews.Search />
<DataViews.FiltersToggle />
<DataViews.FiltersToggled />
<DataViews.Layout />
<DataViews.Pagination />
</DataViews>
);
}
function Save() {
return null;
}
registerBlockType('gateway/dataview', {
edit: Edit,
save: Save,
});
You must be logged in to reply to this topic.