Skip to content

Commit a0bbdc4

Browse files
committed
Initialize isLoading as true when using a promiseFn because that makes sense in most cases.
1 parent 3e33e07 commit a0bbdc4

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/index.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import React from "react"
22

3+
const isFunction = arg => typeof arg === "function"
4+
35
/**
46
* createInstance allows you to create instances of Async that are bound to a specific promise.
57
* A unique instance also uses its own React context for better nesting capability.
@@ -16,7 +18,7 @@ export const createInstance = (defaultProps = {}) => {
1618
this.state = {
1719
data: undefined,
1820
error: undefined,
19-
isLoading: false,
21+
isLoading: isFunction(props.promiseFn) || isFunction(defaultProps.promiseFn),
2022
startedAt: undefined,
2123
finishedAt: undefined,
2224
cancel: this.cancel,
@@ -94,7 +96,7 @@ export const createInstance = (defaultProps = {}) => {
9496

9597
render() {
9698
const { children } = this.props
97-
if (typeof children === "function") {
99+
if (isFunction(children)) {
98100
return <Provider value={this.state}>{children(this.state)}</Provider>
99101
}
100102
if (children !== undefined && children !== null) {
@@ -116,7 +118,7 @@ export const createInstance = (defaultProps = {}) => {
116118
if (state.data !== undefined) return null
117119
if (!persist && state.isLoading) return null
118120
if (!persist && state.error !== undefined) return null
119-
return typeof children === "function" ? children(state) : children || null
121+
return isFunction(children) ? children(state) : children || null
120122
}}
121123
</Consumer>
122124
)
@@ -132,7 +134,7 @@ export const createInstance = (defaultProps = {}) => {
132134
{state => {
133135
if (!state.isLoading) return null
134136
if (initial && state.data !== undefined) return null
135-
return typeof children === "function" ? children(state) : children || null
137+
return isFunction(children) ? children(state) : children || null
136138
}}
137139
</Consumer>
138140
)
@@ -148,7 +150,7 @@ export const createInstance = (defaultProps = {}) => {
148150
{state => {
149151
if (state.data === undefined) return null
150152
if (state.isLoading && !persist) return null
151-
return typeof children === "function" ? children(state.data, state) : children || null
153+
return isFunction(children) ? children(state.data, state) : children || null
152154
}}
153155
</Consumer>
154156
)
@@ -164,7 +166,7 @@ export const createInstance = (defaultProps = {}) => {
164166
{state => {
165167
if (state.error === undefined) return null
166168
if (state.isLoading && !persist) return null
167-
return typeof children === "function" ? children(state.error, state) : children || null
169+
return isFunction(children) ? children(state.error, state) : children || null
168170
}}
169171
</Consumer>
170172
)

src/spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ test("passes isLoading boolean while the promise is running", async () => {
4646
</Async>
4747
)
4848
await waitForElement(() => getByText("done"))
49-
expect(states).toEqual([false, true, false])
49+
expect(states).toEqual([true, true, false])
5050
})
5151

5252
test("passes startedAt date when the promise starts", async () => {

0 commit comments

Comments
 (0)