File tree Expand file tree Collapse file tree
packages/react-reconciler/src/__tests__ Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -1869,4 +1869,53 @@ describe('ReactHooks', () => {
18691869 Scheduler . flushAll ( ) ;
18701870 expect ( root ) . toMatchRenderedOutput ( 'hello' ) ;
18711871 } ) ;
1872+
1873+ // Regression test for https://github.com/facebook/react/issues/15732
1874+ it ( 'resets hooks when an error is thrown in the middle of a list of hooks' , async ( ) => {
1875+ const { useEffect, useState} = React ;
1876+
1877+ class ErrorBoundary extends React . Component {
1878+ state = { hasError : false } ;
1879+
1880+ static getDerivedStateFromError ( ) {
1881+ return { hasError : true } ;
1882+ }
1883+
1884+ render ( ) {
1885+ return (
1886+ < Wrapper >
1887+ { this . state . hasError ? 'Error!' : this . props . children }
1888+ </ Wrapper >
1889+ ) ;
1890+ }
1891+ }
1892+
1893+ function Wrapper ( { children} ) {
1894+ return children ;
1895+ }
1896+
1897+ let setShouldThrow ;
1898+ function Thrower ( ) {
1899+ const [ shouldThrow , _setShouldThrow ] = useState ( false ) ;
1900+ setShouldThrow = _setShouldThrow ;
1901+
1902+ if ( shouldThrow ) {
1903+ throw new Error ( 'Throw!' ) ;
1904+ }
1905+
1906+ useEffect ( ( ) => { } , [ ] ) ;
1907+
1908+ return 'Throw!' ;
1909+ }
1910+
1911+ const root = ReactTestRenderer . create (
1912+ < ErrorBoundary >
1913+ < Thrower />
1914+ </ ErrorBoundary > ,
1915+ ) ;
1916+
1917+ expect ( root ) . toMatchRenderedOutput ( 'Throw!' ) ;
1918+ act ( ( ) => setShouldThrow ( true ) ) ;
1919+ expect ( root ) . toMatchRenderedOutput ( 'Error!' ) ;
1920+ } ) ;
18721921} ) ;
You can’t perform that action at this time.
0 commit comments