Skip to content

Commit eac99b5

Browse files
committed
Support custom displayName when using createInstance.
1 parent 57f7634 commit eac99b5

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ import { createInstance } from "react-async"
139139

140140
const loadCustomer = ({ customerId }) => fetch(`/api/customers/${customerId}`).then(...)
141141

142-
const AsyncCustomer = createInstance({ promiseFn: loadCustomer })
142+
// createInstance takes a defaultProps object and a displayName (both optional)
143+
const AsyncCustomer = createInstance({ promiseFn: loadCustomer }, "AsyncCustomer")
143144

144145
const MyComponent = () => (
145146
<AsyncCustomer customerId="123">

src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const isFunction = arg => typeof arg === "function"
66
* createInstance allows you to create instances of Async that are bound to a specific promise.
77
* A unique instance also uses its own React context for better nesting capability.
88
*/
9-
export const createInstance = (defaultProps = {}) => {
9+
export const createInstance = (defaultProps = {}, displayName = "Async") => {
1010
const { Consumer, Provider } = React.createContext()
1111

1212
class Async extends React.Component {
@@ -203,6 +203,12 @@ export const createInstance = (defaultProps = {}) => {
203203
Async.Resolved = Resolved
204204
Async.Rejected = Rejected
205205

206+
Async.displayName = displayName
207+
Async.Pending.displayName = `${displayName}.Pending`
208+
Async.Loading.displayName = `${displayName}.Loading`
209+
Async.Resolved.displayName = `${displayName}.Resolved`
210+
Async.Rejected.displayName = `${displayName}.Rejected`
211+
206212
return Async
207213
}
208214

0 commit comments

Comments
 (0)