Skip to content

Commit 29ef591

Browse files
authored
Merge pull request #22 from LeetCode-OpenSource/replace-useMemo-with-useEffect
fix the wrong usages of useMemo
2 parents c2c2b82 + a4da1c1 commit 29ef591

File tree

6 files changed

+41
-34
lines changed

6 files changed

+41
-34
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
"jest": "^23.6.0",
3434
"lint-staged": "^8.0.5",
3535
"prettier": "^1.15.2",
36-
"react": "16.7.0-alpha.2",
37-
"react-dom": "16.7.0-alpha.2",
38-
"react-test-renderer": "^16.7.0-alpha.2",
36+
"react": "16.8.0-alpha.1",
37+
"react-dom": "16.8.0-alpha.1",
38+
"react-test-renderer": "16.8.0-alpha.1",
3939
"rxjs": "^6.3.3",
4040
"sinon": "^7.1.1",
4141
"source-map-loader": "^0.2.4",

src/__test__/use-event-callback.spec.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'
22
import { Observable, of, Observer } from 'rxjs'
3-
import { mapTo, delay, withLatestFrom, map } from 'rxjs/operators'
3+
import { mapTo, delay, withLatestFrom, combineLatest, map } from 'rxjs/operators'
44
import { create } from 'react-test-renderer'
55
import * as Sinon from 'sinon'
66

@@ -138,7 +138,7 @@ describe('useEventCallback specs', () => {
138138
_state$: Observable<number>,
139139
) =>
140140
event$.pipe(
141-
withLatestFrom(inputs$),
141+
combineLatest(inputs$),
142142
map(([_, [count]]) => {
143143
return value + count
144144
}),
@@ -167,6 +167,7 @@ describe('useEventCallback specs', () => {
167167
button.props.onClick()
168168
timer.tick(timeToDelay)
169169
testRenderer.update(<Fixture count={4} />)
170+
timer.tick(timeToDelay)
170171
expect(find(testRenderer.root, 'h1').children).toEqual([`${value + 4}`])
171172
timer.restore()
172173
})

src/__test__/use-observable.spec.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,11 @@ describe('useObservable specs', () => {
117117
})
118118

119119
it('should emit changed props in observableFactory', () => {
120+
const timer = Sinon.useFakeTimers()
120121
const spy = Sinon.spy()
122+
const timeToDelay = 200
121123
function Fixture(props: { foo: number; bar: string; baz: any }) {
122-
const value = useObservable((inputs$: Observable<[number, any]>) => inputs$.pipe(tap(spy)), null, [
124+
const value = useObservable((inputs$: Observable<[number, any] | null>) => inputs$.pipe(tap(spy)), null, [
123125
props.foo,
124126
props.baz,
125127
] as any)
@@ -145,13 +147,17 @@ describe('useObservable specs', () => {
145147
expect(find(testRenderer.root, 'div').children).toEqual([])
146148
const newProps = { ...props, bar: 'new bar' }
147149
testRenderer.update(<Fixture {...newProps} />)
150+
// wait useEffect fired
151+
// https://reactjs.org/docs/hooks-reference.html#timing-of-effects
152+
timer.tick(timeToDelay)
148153
expect(spy.callCount).toBe(1)
149154
expect(spy.args[0]).toEqual([[newProps.foo, newProps.baz]])
150155
expect(find(testRenderer.root, 'h1').children).toEqual([`${newProps.foo}`])
151156
expect(find(testRenderer.root, 'div').children).toEqual([`${newProps.baz.foo}`])
152157

153158
const renewProps = { ...props, foo: 1000 }
154159
testRenderer.update(<Fixture {...renewProps} />)
160+
timer.tick(timeToDelay)
155161

156162
expect(spy.callCount).toBe(2)
157163
expect(spy.args[1]).toEqual([[renewProps.foo, renewProps.baz]])

src/use-event-callback.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useMemo, useState } from 'react'
1+
import { useEffect, useState } from 'react'
22
import { Observable, BehaviorSubject, Subject, noop } from 'rxjs'
33

44
import { RestrictArray, VoidAsNull } from './type'
@@ -46,9 +46,9 @@ export function useEventCallback<EventValue, State = void, Inputs = void>(
4646
const [state$] = useState(stateSubject$)
4747
const [inputs$] = useState(inputSubject$)
4848

49-
useMemo(() => {
49+
useEffect(() => {
5050
inputs$.next(inputs!)
51-
}, ((inputs as unknown) as ReadonlyArray<any>) || [])
51+
}, inputs || [])
5252

5353
useEffect(
5454
() => {

src/use-observable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function useObservable<State, Inputs extends ReadonlyArray<any>>(
3232
}
3333
}, [])
3434

35-
useMemo(() => {
35+
useEffect(() => {
3636
inputs$.next(inputs)
3737
}, inputs || [])
3838

yarn.lock

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5634,40 +5634,40 @@ rc@^1.2.7:
56345634
minimist "^1.2.0"
56355635
strip-json-comments "~2.0.1"
56365636

5637-
react-dom@16.7.0-alpha.2:
5638-
version "16.7.0-alpha.2"
5639-
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.7.0-alpha.2.tgz#16632880ed43676315991d8b412cce6975a30282"
5640-
integrity sha512-o0mMw8jBlwHjGZEy/vvKd/6giAX0+skREMOTs3/QHmgi+yAhUClp4My4Z9lsKy3SXV+03uPdm1l/QM7NTcGuMw==
5637+
react-dom@16.8.0-alpha.1:
5638+
version "16.8.0-alpha.1"
5639+
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.8.0-alpha.1.tgz#dab73b8354ba2e498e3127d18e29d4546cea889e"
5640+
integrity sha512-tZCUM8BpnwUHJmLnUWP9c3vVZxnCqYotj7s4tx7umojG6BKv745KIBtuPTzt0EI0q50GMLEpmT/CPQ8iA61TwQ==
56415641
dependencies:
56425642
loose-envify "^1.1.0"
56435643
object-assign "^4.1.1"
56445644
prop-types "^15.6.2"
5645-
scheduler "^0.12.0-alpha.2"
5645+
scheduler "^0.13.0-alpha.1"
56465646

5647-
react-is@^16.7.0-alpha.2:
5648-
version "16.7.0-alpha.2"
5649-
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.7.0-alpha.2.tgz#0dd7f95d45ad5318b7f7bcb99dcb84da9385cb57"
5650-
integrity sha512-1Q3qN8nMWUfFcRz/bBC1f9zSL3il9OcSxMd9CNnpJbeFf4VCX0qYxL3TuwT4f+tFk1TkidwIL11yYgk4HjldYg==
5647+
react-is@^16.8.0-alpha.1:
5648+
version "16.8.0-alpha.1"
5649+
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.8.0-alpha.1.tgz#ac1aed207d6040f002b645af36702edf9ce2c40d"
5650+
integrity sha512-Gsh2u4ovhS2DY6fWgie/av5vzrIfW6P0lgWAsAQp9DjOImE0fJ26FfEdpFXtYBwi5s2krT9z0xvcQKvQsi4ekw==
56515651

5652-
react-test-renderer@^16.7.0-alpha.2:
5653-
version "16.7.0-alpha.2"
5654-
resolved "https://registry.yarnpkg.com/react-test-renderer/-/react-test-renderer-16.7.0-alpha.2.tgz#8606a5a82240c405539da0401d7b3572898b5611"
5655-
integrity sha512-taA9MrHMi7hEM/cKgvcvht+9cszhPirCaSP99yxkVQ2JwQxYSltGYq2gFf/UQBqGJMgmgEghN62rxziaL1EK+A==
5652+
react-test-renderer@16.8.0-alpha.1:
5653+
version "16.8.0-alpha.1"
5654+
resolved "https://registry.yarnpkg.com/react-test-renderer/-/react-test-renderer-16.8.0-alpha.1.tgz#65f09023aeb83cbb1716f2364fed61c0017cd2ba"
5655+
integrity sha512-ayhWv47RYZuZ/vY/Asmf2qLgphHtwsq32Y7uNztyO4HcdFh6HrieDioYwbObfxl3+Uf3r4UezJ7uxqT5NndsHw==
56565656
dependencies:
56575657
object-assign "^4.1.1"
56585658
prop-types "^15.6.2"
5659-
react-is "^16.7.0-alpha.2"
5660-
scheduler "^0.12.0-alpha.2"
5659+
react-is "^16.8.0-alpha.1"
5660+
scheduler "^0.13.0-alpha.1"
56615661

5662-
react@16.7.0-alpha.2:
5663-
version "16.7.0-alpha.2"
5664-
resolved "https://registry.yarnpkg.com/react/-/react-16.7.0-alpha.2.tgz#924f2ae843a46ea82d104a8def7a599fbf2c78ce"
5665-
integrity sha512-Xh1CC8KkqIojhC+LFXd21jxlVtzoVYdGnQAi/I2+dxbmos9ghbx5TQf9/nDxc4WxaFfUQJkya0w1k6rMeyIaxQ==
5662+
react@16.8.0-alpha.1:
5663+
version "16.8.0-alpha.1"
5664+
resolved "https://registry.yarnpkg.com/react/-/react-16.8.0-alpha.1.tgz#c2b32689f3b466d3ce85a634dd9035f789d2cd97"
5665+
integrity sha512-vLwwnhM2dXrCsiQmcSxF2UdZVV5xsiXjK5Yetmy8dVqngJhQ3aw3YJhZN/YmyonxwdimH40wVqFQfsl4gSu2RA==
56665666
dependencies:
56675667
loose-envify "^1.1.0"
56685668
object-assign "^4.1.1"
56695669
prop-types "^15.6.2"
5670-
scheduler "^0.12.0-alpha.2"
5670+
scheduler "^0.13.0-alpha.1"
56715671

56725672
read-pkg-up@^1.0.1:
56735673
version "1.0.1"
@@ -6022,10 +6022,10 @@ sax@^1.2.4:
60226022
resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9"
60236023
integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==
60246024

6025-
scheduler@^0.12.0-alpha.2:
6026-
version "0.12.0-alpha.2"
6027-
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.12.0-alpha.2.tgz#2a8bc8dc6ecdb75fa6480ceeedc1f187c9539970"
6028-
integrity sha512-bfqFzGH18MjjhePIzYQNR0uGQ1wMCX6Q83c2s+3fzyuqKT6zBI2wNQTpq01q72C7QItAp8if5w2LfMiXnI2SYw==
6025+
scheduler@^0.13.0-alpha.1:
6026+
version "0.13.0-alpha.1"
6027+
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.13.0-alpha.1.tgz#753977fb4fb35d8cdd559868a11e46b640955556"
6028+
integrity sha512-W0sH0848sVuPKg+I18vTYQyzVtA4X1lrVgSeXK6KnOPUltFdJcY5nkbTkjGUeS/E0x+eBsNYfSdhJtGjT95njw==
60296029
dependencies:
60306030
loose-envify "^1.1.0"
60316031
object-assign "^4.1.1"

0 commit comments

Comments
 (0)