TableView
The TableView
component renders the contents of a single Table
in a Store
, and registers a listener so that any changes to that result will cause a re-render.
TableView(props: TableProps): ComponentReturnType
Type | Description | |
---|---|---|
props | TableProps | The props for this component. |
returns | ComponentReturnType | A rendering of the |
The component's props identify which Table
to render based on Table
Id
, and Store
(which is either the default context Store
, a named context Store
, or by explicit reference).
This component renders a Table
by iterating over its Row
objects. By default these are in turn rendered with the RowView
component, but you can override this behavior by providing a rowComponent
prop, a custom component of your own that will render a Row
based on RowProps
. You can also pass additional props to your custom component with the getRowComponentProps
callback prop.
You can create your own TableView-like component to customize the way that a Table
is rendered: see the TablesView
component for more details.
This component uses the useRowIds
hook under the covers, which means that any changes to the structure of the Table
will cause a re-render.
Since v4.1.0, you can use the customCellIds
prop if you want to render a prescribed set of the Table
's Cells in a given order for each Row
.
Examples
This example creates a Store
outside the application, which is used in the TableView
component by reference. A change to the data in the Store
re-renders the component.
import React from 'react';
import {TableView} from 'tinybase/ui-react';
import {createRoot} from 'react-dom/client';
import {createStore} from 'tinybase';
const store = createStore().setTable('pets', {fido: {species: 'dog'}});
const App = () => (
<div>
<TableView tableId="pets" store={store} separator="/" />
</div>
);
const app = document.createElement('div');
createRoot(app).render(<App />);
console.log(app.innerHTML);
// -> '<div>dog</div>'
store.setRow('pets', 'felix', {species: 'cat'});
console.log(app.innerHTML);
// -> '<div>dog/cat</div>'
This example creates a Provider context into which a default Store
is provided. The TableView
component within it then renders the Table
for a custom set of Cell
Ids
(and rendered with Ids
for readability).
import {Provider, TableView} from 'tinybase/ui-react';
import React from 'react';
import {createRoot} from 'react-dom/client';
import {createStore} from 'tinybase';
const App = ({store}) => (
<Provider store={store}>
<Pane />
</Provider>
);
const customCellIds = ['species'];
const Pane = () => (
<div>
<TableView
tableId="pets"
customCellIds={customCellIds}
debugIds={true}
/>
</div>
);
const store = createStore().setTable('pets', {
fido: {color: 'black', species: 'dog'},
felix: {color: 'brown', species: 'cat'},
});
const app = document.createElement('div');
createRoot(app).render(<App store={store} />);
console.log(app.innerHTML);
// -> '<div>pets:{fido:{species:{dog}}felix:{species:{cat}}}</div>'
This example creates a Provider context into which a default Store
is provided. The TableView
component within it then renders the Table
with a custom Row
component and a custom props callback.
import {Provider, RowView, TableView} from 'tinybase/ui-react';
import React from 'react';
import {createRoot} from 'react-dom/client';
import {createStore} from 'tinybase';
const App = ({store}) => (
<Provider store={store}>
<Pane />
</Provider>
);
const getBoldProp = (rowId) => ({bold: rowId == 'fido'});
const Pane = () => (
<div>
<TableView
tableId="pets"
rowComponent={FormattedRowView}
getRowComponentProps={getBoldProp}
/>
</div>
);
const FormattedRowView = ({tableId, rowId, bold}) => (
<span>
{bold ? <b>{rowId}</b> : rowId}
{': '}
<RowView tableId={tableId} rowId={rowId} />
</span>
);
const store = createStore().setTable('pets', {
fido: {species: 'dog'},
felix: {species: 'cat'},
});
const app = document.createElement('div');
createRoot(app).render(<App store={store} />);
console.log(app.innerHTML);
// -> '<div><span><b>fido</b>: dog</span><span>felix: cat</span></div>'
Since
v1.0.0