mirror of
https://github.com/re-rxjs/react-rxjs.git
synced 2025-12-08 18:01:51 +00:00
@react-rxjs/core: fix StrictMode error on immediate unmount
This commit is contained in:
parent
26b6460c77
commit
af85c8a567
@ -1,6 +1,6 @@
|
||||
import { state } from "@rxstate/core"
|
||||
import { render, screen } from "@testing-library/react"
|
||||
import React, { StrictMode, useState } from "react"
|
||||
import React, { StrictMode, useState, useEffect } from "react"
|
||||
import { defer, EMPTY, NEVER, Observable, of, startWith } from "rxjs"
|
||||
import { bind, RemoveSubscribe, Subscribe as OriginalSubscribe } from "./"
|
||||
import { TestErrorBoundary } from "./test-helpers/TestErrorBoundary"
|
||||
@ -266,6 +266,43 @@ describe("Subscribe", () => {
|
||||
expect(getByTestId("id").textContent).toBe("1")
|
||||
expect(getByTestId("value").textContent).toBe("1")
|
||||
})
|
||||
|
||||
it("on StrictMode: it doesn't crash if the component immediately unmounts", () => {
|
||||
function App() {
|
||||
const [switched, setSwitched] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
setSwitched(true)
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<div className="App">
|
||||
{switched ? <SwitchToComponent /> : <ProblematicComponent />}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const ProblematicComponent = () => {
|
||||
return <Subscribe></Subscribe>
|
||||
}
|
||||
const SwitchToComponent = () => {
|
||||
return <div>All good</div>
|
||||
}
|
||||
|
||||
let hasError = false
|
||||
|
||||
render(
|
||||
<TestErrorBoundary
|
||||
onError={() => {
|
||||
hasError = true
|
||||
}}
|
||||
>
|
||||
<App />
|
||||
</TestErrorBoundary>,
|
||||
)
|
||||
|
||||
expect(hasError).toBe(false)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@ -77,7 +77,7 @@ export const Subscribe: React.FC<{
|
||||
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
subscriptionRef.current!.unsubscribe()
|
||||
subscriptionRef.current?.unsubscribe()
|
||||
subscriptionRef.current = undefined
|
||||
}
|
||||
}, [])
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user