Skip to content
This repository has been archived by the owner on Dec 31, 2020. It is now read-only.

Commit

Permalink
Revert "Make StrictMode happy (#119)" (#145)
Browse files Browse the repository at this point in the history
This reverts commit bf3a6c9.
  • Loading branch information
FredyC authored May 1, 2019
1 parent cf5b97a commit 36657cc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 57 deletions.
20 changes: 6 additions & 14 deletions src/useObserver.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Reaction } from "mobx"
import { useDebugValue, useEffect, useRef } from "react"
import { useDebugValue, useRef } from "react"
import { printDebugValue } from "./printDebugValue"
import { isUsingStaticRendering } from "./staticRendering"
import { useForceUpdate } from "./utils"
import { useForceUpdate, useUnmount } from "./utils"

export type ForceUpdateHook = () => () => void

Expand All @@ -25,25 +25,17 @@ export function useObserver<T>(
const forceUpdate = wantedForceUpdateHook()

const reaction = useRef<Reaction | null>(null)
const committed = useRef(false)

if (!reaction.current) {
// First render for this component. Not yet committed.
reaction.current = new Reaction(`observer(${baseComponentName})`, () => {
// Observable has changed. Only force an update if we've definitely
// been committed.
if (committed.current) {
forceUpdate()
}
forceUpdate()
})
}

useDebugValue(reaction, printDebugValue)

useEffect(() => {
committed.current = true
return () => reaction.current!.dispose()
}, [])
useUnmount(() => {
reaction.current!.dispose()
})

// render the original component, but have the
// reaction track the observables, so that rendering
Expand Down
8 changes: 7 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { useCallback, useState } from "react"
import { useCallback, useEffect, useState } from "react"

const EMPTY_ARRAY: any[] = []

export function useUnmount(fn: () => void) {
useEffect(() => fn, EMPTY_ARRAY)
}

export function useForceUpdate() {
const [, setTick] = useState(0)
Expand Down
42 changes: 0 additions & 42 deletions test/useObserver.test.tsx

This file was deleted.

0 comments on commit 36657cc

Please sign in to comment.