-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Patrick Burtchaell
authored
Feb 3, 2019
1 parent
0b61389
commit 612043c
Showing
23 changed files
with
9,834 additions
and
8,759 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# Upgrade from 5.x to 6.0.0 | ||
|
||
This version includes changes to the public API, making it easier to import and use the middleware. | ||
|
||
## New: Preconfigured by Default | ||
|
||
### Before | ||
|
||
Previously, the middleware need to be instantiated with an optional configuration. | ||
|
||
```js | ||
import promiseMiddleware from 'redux-promise-middleware' | ||
|
||
applyMiddleware( | ||
promiseMiddleware({ | ||
// Optional configuration | ||
}), | ||
)(createStore) | ||
``` | ||
|
||
This implementation enabled custom configuration, but, for most implementations, it is uncessary overhead. | ||
|
||
### After | ||
|
||
Now, the default export is preconfigured and ready to go. | ||
|
||
```js | ||
import promise from 'redux-promise-middleware' | ||
|
||
applyMiddleware( | ||
promise, | ||
)(createStore) | ||
``` | ||
|
||
The middleware still supports custom configuration: import `createPromise` and pass in the configuration object. | ||
|
||
```js | ||
import { createPromise } from 'redux-promise-middleware' | ||
|
||
applyMiddleware( | ||
createPromise({ | ||
// Custom configuration | ||
typeDelimiter: '/', | ||
}), | ||
)(createStore) | ||
``` | ||
|
||
## New: `ActionType` Export | ||
|
||
### Before | ||
|
||
|
||
```js | ||
import { PENDING, FULFILLED, REJECTED } from 'redux-promise-middleware' | ||
``` | ||
|
||
Previously, the middleware exported three string constants. One each for the pending, fulfilled and rejected action types. This is useful for reducers, for example: | ||
|
||
```js | ||
const reducer = (state = {}, action) => { | ||
switch(action.type) { | ||
case `FOO_${PENDING}`: | ||
// .. | ||
|
||
case `FOO_${FULFILLED}`: | ||
// ... | ||
|
||
default: return state; | ||
} | ||
} | ||
``` | ||
|
||
This is a nice affordance, it could be better design if the action types were exported as an enum (E.g., an object, since this is just JavaScript), as opposed three individual strings. | ||
|
||
### After | ||
|
||
```js | ||
import { ActionType } from 'redux-promise-middleware' | ||
``` | ||
|
||
Now, the action types are exported as one enum. | ||
|
||
```js | ||
const reducer = (state = {}, action) => { | ||
switch(action.type) { | ||
case `FOO_${ActionTypes.Pending}`: | ||
// .. | ||
|
||
case `FOO_${ActionTypes.Fulfilled}`: | ||
// ... | ||
|
||
case `FOO_${ActionTypes.Rejected}`: | ||
// ... | ||
|
||
default: return state; | ||
} | ||
} | ||
``` | ||
|
||
Using action types is entirely optional. One one hand, code benefits from more robust types and is less prone to static errors, but, on another hand, you and your team spends more time and effort writing the code. | ||
|
||
At the end of the day, you can also just use regular strings like any other action. | ||
|
||
|
||
```js | ||
const reducer = (state = {}, action) => { | ||
switch(action.type) { | ||
case `FOO_PENDING`: | ||
// .. | ||
|
||
case `FOO_FULFILLED`: | ||
// ... | ||
|
||
case `FOO_REJECTED`: | ||
// ... | ||
|
||
default: return state; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.cache | ||
dist | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Using Promise Middleware with TypeScript | ||
|
||
This example demonstrates how to use the middleware with TypeScript. | ||
|
||
## Getting Started | ||
|
||
- Clone this repository to your computer | ||
- Open the folder for this example | ||
- Run `npm i` to install dependencies | ||
- Run `npm start` to start the example | ||
- Open `http://localhost:1234` in a web browser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!doctype html public> | ||
<body> | ||
<style>button { margin: 5px 0 10px 0; } img { max-width: 200px; }</style> | ||
<title>Using Redux Promise Middleware</title> | ||
<h1>Using Redux Promise Middleware</h1> | ||
<p>This example demonstrates how to use the middleware to fetch data a JSON API.</p> | ||
<hr /> | ||
<button id="load">Get Post</button> | ||
<div id="mount"/> | ||
<script async src="./index.ts"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { AsyncAction } from 'redux-promise-middleware'; | ||
import store, { State } from './store'; | ||
|
||
/** | ||
* @private | ||
* Fetch an image of a dog from the [Dog API](https://dog.ceo/dog-api/) | ||
*/ | ||
const getDog = (): AsyncAction => ({ | ||
type: 'GET_DOG', | ||
payload: fetch('https://dog.ceo/api/breeds/image/random') | ||
.then(response => response.json()), | ||
}); | ||
|
||
/** | ||
* @private | ||
* Renders the given state to the given HTML DOM node | ||
*/ | ||
const render = (mount: HTMLElement | null, state: State) => { | ||
if (mount && state.isPending) { | ||
mount.innerHTML = 'Loading...'; | ||
} else if (mount && state.image) { | ||
mount.innerHTML = `<img src=${state.image} />`; | ||
} | ||
}; | ||
|
||
/** | ||
* @private | ||
* Renders the initial state of the example | ||
*/ | ||
const initialize = () => { | ||
const mount: HTMLElement | null = document.querySelector('#mount'); | ||
|
||
// Load the post when button is clicked | ||
const button: HTMLElement | null = document.querySelector('#load'); | ||
if (button) button.addEventListener('click', () => store.dispatch(getDog())); | ||
|
||
render(mount, store.getState()); | ||
store.subscribe(() => render(mount, store.getState())); | ||
}; | ||
|
||
initialize(); |
Oops, something went wrong.