concept WeekPicker in category react

appears as: WeekPicker, WeekPicker
React Hooks in Action

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

In this chapter, we start by addressing a problem with the BookablesList component in the bookings app where there’s something amiss with our state management. We then introduce reducers and the useReducer hook as a way of managing our state. Section 4.3 shows how to use a function to initialize the state for a reducer as we start work on a new component, the WeekPicker. We finish the chapter with a review of how the useReducer hook fits in with our understanding of function components.

Listing 8.5 The Bookings component
import React, {useState, useReducer} from 'react';
import {getWeek} from "../../utils/date-wrangler";
 
import WeekPicker from "../WeekPicker";
import BookingsGrid from "./BookingsGrid";
import BookingDetails from "./BookingDetails";
 
import weekReducer from "../WeekPicker/reducer";                      #A
 
export default function Bookings ({bookable}) {                       #B
 
const [week, dispatch] = useReducer(                                #C
  weekReducer, new Date(), getWeek
);
 
  const [booking, setBooking] = useState(null);                       #D
 
  return (
    <div className="bookings">
      <div>
        <WeekPicker
          dispatch={dispatch}
        />
 
        <BookingsGrid
          week={week}
          bookable={bookable}
          booking={booking}
          setBooking={setBooking}
        />
      </div>
 
      <BookingDetails
        booking={booking}
        bookable={bookable}
      />
    </div>
  )
}