Moped Documentation
  • Welcome 👋
  • User Guides
    • Getting started
    • Map a project
  • Product Management
    • User communication
    • User management
    • User analytics
    • Local testing
    • Release process
    • Patch release process
    • MUI X Pro License
    • Integrations
      • Dataset documentation
      • ArcGIS Online
      • eCapris
      • Power BI
    • Features
  • Dev Guides
    • DB Docs & Data dictionary
    • Database backup policy
    • Moped Read Replica
    • How-to's
      • How do I start the Hasura cluster locally?
      • How do I launch the Hasura Console?
      • How do I get a JWT token?
      • How to ping the GraphQL API
      • How to ping the REST API
      • How do I connect a database with Postgres GUIs?
      • How do I connect to the RDS instance?
      • How to load production data into a local instance
      • How do I update seed data?
    • Hasura
      • Hasura Roles
      • Hasura Migrations
        • Getting Started
        • Installing the Hasura CLI
        • Configuration Files
        • Hasura Migration Principles
        • The Migration file format
        • Development
        • Hasura Seed Data
        • Running the Hasura Cluster Locally (video)
        • Create a migration: Exercise 1 (video)
        • Create a migration: Exercise 2 (video)
        • Latest hasura-cluster features
    • User Management
    • Authentication
      • Authentication Architecture
      • DynamoDB & Cognito
      • Secrets Manager & Cognito
      • Hasura & Cognito
      • React & Cognito
      • Flask API & Cognito
      • Single Sign-On with CTM
    • Code organization
    • API
      • Configuration Files
      • Testing
      • User Management API
    • Maps and geospatial data
      • Access tokens and API keys
      • Map libraries
      • Map data
      • Map styles
      • Map layers and basemaps
      • React patterns
      • V1 Archive
        • Map libraries
        • Map data
        • Map custom hooks
        • Map styles
        • Map layers and basemaps
    • UI access control
    • Design system
      • Branding
      • Component styles
      • Text content
    • Activity Log
      • Architecture
      • GitHub Actions and Deployment of Updates
      • Hasura Event Logs and Truncate Cron Job
      • Authentication
  • See also
  • Get Moped support, report a bug, or request an enhancement
  • Data & Technology Services
  • Github repository
Powered by GitBook
On this page

Was this helpful?

  1. Dev Guides
  2. Maps and geospatial data

React patterns

Patterns used to manage map state and query/mutate data

PreviousMap layers and basemapsNextV1 Archive

Last updated 2 years ago

Was this helpful?

useReducer

To organize the create, update, and delete actions that occur while using the project components map, is used to hold and modify state needed for the UI flows and also group state updates into actions organized into switch cases within a reducer function.

For example, to create a component, there are three pieces of state needed to move through the UI.

const [createState, createDispatch] = useReducer(createReducer, {
    isCreatingComponent: false, // track if user is creating
    showCreateDialog: false, // show initial dialog create form
    draftComponent: null, // hold data needed to create record
  });

The reducer function contains many cases that modify different combinations of this state.

For example, when someone starts creating a component and moves past the create form, we need to store what data we have so far. In the code, when the user clicks the Continue button that takes them to the map where they can begin selecting (or drawing) lines or points, we fire off the createDispatch function to update createState:

createDispatch({ type: "store_draft_component", payload: newComponent });

The type message is caught in a switch statement and the state updates like:

const createReducer = (state, action) => {
  switch (action.type) {
      case "store_draft_component":
            return {
                ...state, // the other current createState values
                draftComponent: action.payload, // data about the component
                isCreatingComponent: true, // tell the map code we are creating
            };
// ...the rest of the function

useReducer