1- # Countries
1+ # Countries (React)
22
3- In this demo, we build a simple app that uses React and a simple Store object to
4- load and display country data.
3+ In this demo, we build a simple app that uses React and a simple Store object
4+ to load and display country data.
55
66## Initialization
77
@@ -48,7 +48,6 @@ import {
4848 useCreateIndexes ,
4949 useCreatePersister ,
5050 useCreateStore ,
51- useSetRowCallback ,
5251 useSetValuesCallback ,
5352 useSliceRowIds ,
5453 useValues ,
@@ -66,8 +65,8 @@ const UNSTAR = '\u2606';
6665## Starting The App
6766
6867We have a top-level ` App ` component, in which we initialize our data, and
69- render the parts of the app. Firstly, we create and memoize a set of three Store
70- objects with their schemas:
68+ render the parts of the app. Firstly, we create and memoize a set of three
69+ Store objects with their schemas:
7170
7271- ` countryStore ` contains a list of the world's countries, loaded once from a
7372 JSON file using a remote Persister object.
@@ -88,11 +87,7 @@ const App = () => {
8887 );
8988 useCreatePersister (
9089 countryStore,
91- (store ) =>
92- createRemotePersister (
93- store,
94- ' https://tinybase.org/assets/countries.json' ,
95- ),
90+ (store ) => createRemotePersister (store, ' /assets/countries.json' ),
9691 [],
9792 (persister ) => persister .load (),
9893 );
@@ -132,12 +127,13 @@ const App = () => {
132127 viewStore,
133128 (store ) => createSessionPersister (store, ' countries/viewStore' ),
134129 [],
135- (persister ) => persister .startAutoPersisting ()
130+ (persister ) => persister .startAutoPersisting (),
136131 );
137132 // ...
138133` ` `
139134
140- We also create and memoize two Indexes objects with the useCreateIndexes hook:
135+ We also create and memoize two Indexes objects with the ` useCreateIndexes`
136+ hook:
141137
142138- ` countryIndexes` contains a single Index of countries in ` countryStore` by
143139 their first letter, sorted alphabetically.
@@ -218,9 +214,9 @@ window.addEventListener('load', () =>
218214
219215## The 'Current Slice'
220216
221- At the heart of this app is the concept of the 'current slice': at any one time,
222- the app is displaying the countries present in a specific sliceId of a specific
223- indexId of a specific Indexes object. We store these three ids in the
217+ At the heart of this app is the concept of the 'current slice': at any one
218+ time, the app is displaying the countries present in a specific sliceId of a
219+ specific indexId of a specific Indexes object. We store these three ids in the
224220` viewStore ` as keyed values so they persist between reloads.
225221
226222Since both the left-hand and right-hand panels of the app need to read these
@@ -247,19 +243,19 @@ const useSetCurrentSlice = (indexes, indexId, sliceId) =>
247243## The ` Filters ` Component
248244
249245This component provides the list of countries' first letters down the left-hand
250- side of the app. We actually build this as an IndexView component that lists all
251- the ` sliceIds ` in the ` countryIndexes ` index, but also add an explicit item at
252- the top of the list to allow the user to select starred countries from the
246+ side of the app. We actually build this as an ` IndexView ` component that lists
247+ all the ` sliceIds ` in the ` countryIndexes ` index, but also add an explicit item
248+ at the top of the list to allow the user to select starred countries from the
253249` starIndexes ` index.
254250
255251The custom ` useCurrentSlice ` hook is used to get the current Indexes object
256252name, current indexId, and current sliceId. We use these to determine whether a
257253Filter is selected, and that flag is passed down as the ` selected ` prop to each
258254of the child Filter components so they know whether to display themselves as
259- selected or not. We could have each letter of the side bar listening for changes
260- to the current slice, but in this case it is more efficient to do it once and
261- pass down the ` currentSlice ` as a prop, using the ` getSliceComponentProps `
262- callback:
255+ selected or not. We could have each letter of the side bar listening for
256+ changes to the current slice, but in this case it is more efficient to do it
257+ once and pass down the ` currentSlice ` as a prop, using the
258+ ` getSliceComponentProps ` callback:
263259
264260``` jsx
265261const Filters = () => {
@@ -302,23 +298,23 @@ const Filters = () => {
302298```
303299
304300Each letter in the left hand ` Filters ` component is a ` Filter ` component, which
305- knows which Indexes object the app needs to show, along with the index and slice
306- Ids. This is set with the callback returned by the ` useSetCurrentSlice ` custom
307- hook.
301+ knows which Indexes object the app needs to show, along with the index and
302+ slice Ids. This is set with the callback returned by the ` useSetCurrentSlice `
303+ custom hook.
308304
309305For example, clicking the letter 'N' will set the current named Indexes object
310- to be ` countryIndexes ` , the current indexId to be ` firstLetter ` , and the current
311- sliceId to be 'N'. Clicking the star at the to of the list will set the current
312- named Indexes object to be ` starIndexes ` , the current indexId to be ` star ` , and
313- the current sliceId to be 'true'.
306+ to be ` countryIndexes ` , the current indexId to be ` firstLetter ` , and the
307+ current sliceId to be 'N'. Clicking the star at the top of the list will set
308+ the current named Indexes object to be ` starIndexes ` , the current indexId to be
309+ ` star ` , and the current sliceId to be 'true'.
314310
315311The ` currentSlice ` prop passed down from the ` Filters ` component is used to
316312decide whether to style the letter as the 'current' selection.
317313
318314We also display the number of countries in the slice of the relevant index.
319315Instead of setting up a Metrics object to track this, it's simpler to just use
320- the useSliceRowIds hook and show the ` length ` of the resulting array. Only the
321- count of starred countries changes during the life of the app anyway:
316+ the ` useSliceRowIds ` hook and show the ` length ` of the resulting array. Only
317+ the count of starred countries changes during the life of the app anyway:
322318
323319``` jsx
324320const Filter = ({
@@ -369,11 +365,12 @@ These filters also have some straightforward styling:
369365
370366## The ` Countries ` Component
371367
372- The main right-hand side of the app is a panel that shows the view selected with
373- the left-hand ` Filters ` component. As we have seen, that component is setting
374- the 'current slice' to be shown, comprising the name of the Indexes object in
375- focus, an indexId, and a sliceId. We use those three parameters directly as the
376- props for the SliceView component that forms the main part of the app:
368+ The main right-hand side of the app is a panel that shows the view selected
369+ with the left-hand ` Filters ` component. As we have seen, that component is
370+ setting the 'current slice' to be shown, comprising the name of the Indexes
371+ object in focus, an indexId, and a sliceId. We use those three parameters
372+ directly as the props for the ` SliceView ` component that forms the main part of
373+ the app:
377374
378375``` jsx
379376const Countries = () => (
@@ -451,5 +448,6 @@ the country cards and flags to look good!
451448}
452449```
453450
454- And that's it! A simple app, all in all, but one that demonstrates using Indexes
455- objects and passing down props to build a useful stateful user interface.
451+ And that's it! A simple app, all in all, but one that demonstrates using
452+ Indexes objects and passing down props to build a useful stateful user
453+ interface.
0 commit comments