concept group in category react

appears as: groups, group, group, groups
React Hooks in Action

This is an excerpt from Manning's book React Hooks in Action.

Suspense allows the developer to deliberately manage loading states for multiple components, either showing fallbacks for individual components, groups of components or the app as a whole. It provides a mechanism for library authors to update their APIs to work with the Suspense component, so their asynchronous features can make full use of the loading state management that Suspense provides.

Figure 4.1 Selecting a bookable, switching groups and then clicking the Next button can lead to unpredictable state changes.
Listing 4.5 The BookablesList component using a reducer
import React, {useReducer, Fragment} from "react";                      #A 
import {bookables, sessions, days} from "../../static.json";
import {FaArrowRight} from "react-icons/fa";
 
import reducer from "./reducer";                                        #B
 
const initialState = {                                                  #C
  group: "Rooms",                                                       #C
  bookableIndex: 0,                                                     #C
  hasDetails: true,                                                     #C
  bookables                                                             #C
};                                                                      #C
 
export default function BookablesList () {
  const [state, dispatch] = useReducer(reducer, initialState);          #D
 
  const {group, bookableIndex, bookables, hasDetails} = state;          #E
 
  const bookablesInGroup = bookables.filter(b => b.group === group);
  const bookable = bookablesInGroup[bookableIndex];
  const groups = [...new Set(bookables.map(b => b.group))];
 
  function changeGroup (e) {
    dispatch({                                                          #F
      type: "SET_GROUP",                                                #F
      payload: e.target.value                                           #F
    });                                                                 #F
  }
 
  function changeBookable (selectedIndex) {
    dispatch({
      type: "SET_BOOKABLE",
      payload: selectedIndex
    });
  } 
 
  function nextBookable () {
    dispatch({ type: "NEXT_BOOKABLE" });                                #G
  }
 
  function toggleDetails () {
    dispatch({ type: "TOGGLE_HAS_DETAILS" });
  }
 
  return (
    <Fragment>
      <div>
        // group picker
 
        <ul className="bookables items-list-nav">
          {bookablesInGroup.map((b, i) => (
            <li
            key={b.title}
            className={i === bookableIndex ? "selected" : null}
            >
              <button
                className="btn"
                onClick={() => changeBookable(i)}                          #H
              >
                {b.title}
              </button>
            </li>
          ))}
        </ul>
 
        // Next button
      </div>
 
      {bookable && (
        <div className="bookable-details">
          <div className="item">
            <div className="item-header">
              <h2>
                {bookable.title}
              </h2>
              <span className="controls">
                <label>
                  <input
                    type="checkbox"
                    checked={hasDetails}
                    onChange={toggleDetails}                              #I
                  />
                  Show Details
                </label>
              </span>
            </div>
            <p>{bookable.notes}</p>
            {hasDetails && (
              <div className="item-details">
                // details
              </div>
            )}
          </div>
        </div>
      )}
    </Fragment>
  );
}

Left alone in Presentation Mode, the component cycles through all of the bookables in a group, wrapping back to the first when it leaves the last. As soon as a user selects a bookable, either by clicking one directly or by using the Next button, the component exits Presentation Mode and only re-enters it if the user refreshes the page. But, choosing a different group, switching from ‘Rooms’ to ‘Kit’ for example, or toggling the display of details by clicking the Show Details checkbox, shouldn’t exit Presentation Mode. Table 6.1 lists the desired behaviors after different events.

After

Continue Presentation Mode

The component loads

Yes

The user selects a bookable

No

The user uses the Next button

No

The user switches groups

Yes

The user toggles bookable details

Yes

We’ll use timers to schedule when the component should move on to the next bookable. If the user switches groups, automatically moving the selected bookable to the first in the new group, we have to cancel the current timer and set up a new one to give the newly selected bookable its allotted time. But, we’re getting ahead of ourselves! One step at a time.