Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
Merge pull request #404 from HospitalRun/redux-to-react-query-adr
Browse files Browse the repository at this point in the history
docs: add adr for switching from redux to react-query
  • Loading branch information
fox1t committed Aug 10, 2020
2 parents c4e9e6b + 57bb02f commit 918bb81
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
8 changes: 4 additions & 4 deletions docs/adr.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## What are architecture decision records?

An architecture decision record (ADR) is a document that documents an architecture decision (AD) a made because of an architecturally-significant non-functional or functional requirement. These decisions are ones that have a large influence on architecture, are fundamental to the way the software is built, or would be difficult to reverse.
An architecture decision record (ADR) is a document that documents an architecture decision (AD) a made because of an architecturally-significant non-functional or functional requirement. These decisions are ones that have a large influence on architecture, are fundamental to the way the software is built, or would be difficult to reverse.

All architecture decision records make up this architecture decision log.

Expand All @@ -23,6 +23,6 @@ All architecture decision records make up this architecture decision log.

## Architecture Decision Log

| Decision | Status | Author |
|--------------------------------|---------------------------|------------------------------|
| | | |
| Decision | Status | Author |
|-----------------------------------------------------------------------|------------------------------------------------------------------|----------------------------------------------------|
| [Migrate Redux to React Query & Context API](./redux_to_react_query) | ![Status](https://img.shields.io/badge/status-proposed-yellow) | [@jackcmeyer](https://github.com/jackcmeyer) |
53 changes: 53 additions & 0 deletions docs/adr/redux_to_react_query.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Migrate to React Query/Context API/Hooks from Redux

## Status
![Status](https://img.shields.io/badge/status-proposed-yellow)

## Context
Currently, HospitalRun Frontend uses [redux](https://react-redux.js.org/) to manage async data
fetching through [redux thunk](https://github.com/reduxjs/redux-thunk).
It also uses redux for handling the business logic. Redux's main use case is for handling and
managing shared application state. HospitalRun Frontend has almost no shared state across components
since it always goes back to PouchDB/CouchDB for the most recent data. Redux code is often verbose and
contains a lot of boilerplate. Although, HospitalRun Frontend uses [redux toolkit](https://redux-toolkit.js.org/),
the application still has a lot of boilerplate. Due to the limited amount of global application
state, the desire to reduce boilerplate redux code, and new/other libraries available,
Redux is no longer the correct tool.

Redux also makes testing more complicated. Since components that display data (i.e. patient data) are
connected to the redux store, a mock redux store must be provided during tests. This makes it
difficult to isolate testing just to the specific component. For components that save data, it
is difficult to mock the actions that are dispatched.

[react-query](https://github.com/tannerlinsley/react-query) is a library for "handling fetching,
caching, and updating asynchronous data in React". This library has become a popular replacement
for the redux pattern of dispatching an action and storing the result of the data fetch in the redux
store.

For the few uses cases that require global application state (i.e. session information),
the [React Context API](https://reactjs.org/docs/context.html) is an alternative from the
React library. The context API is designed for the use case of sharing state across
components.

## Decision
HospitalRun has chosen to use React Query to manage asynchronous requests for fetching data, the
context api to manage shared application state such as user information, and hooks for sharing
code and business logic.

## Impact/Risks/Consequences
Since HospitalRun v2.0 is still in a development state, there is little risk to refactoring to use
react query, context api, and hooks.

There is a significant amount of refactor work to manage, however the outcome will be an application
that is easier to maintain and contribute to.

Since hooks are just functions, it will be significantly easier to test due to being able to mock
the function and use spies.

At the end of this refactor, no redux related code should exist in the application. All code should
be migrated to a combination of react query hooks and context api for application state.

Although data fetches can be done through regular function calls to the PouchDB library, react-query
is able to help provide a consistent interface for returning errors and help with loading states during
async data fetching. React Query also will serve as a common interface for all async data fetches
regardless of the target.

0 comments on commit 918bb81

Please sign in to comment.