-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c56ed2
commit 65f3644
Showing
6 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# `useError` | ||
|
||
React side-effect hook that returns an error dispatcher. | ||
|
||
## Usage | ||
|
||
```jsx | ||
import { useError } from 'react-use'; | ||
|
||
const Demo = () => { | ||
const dispatchError = useError(); | ||
|
||
const clickHandler = () => { | ||
dispatchError(new Error('Some error!')); | ||
}; | ||
|
||
return <button onClick={clickHandler}>Click me to throw</button>; | ||
}; | ||
|
||
// In parent app | ||
const App = () => ( | ||
<ErrorBoundary> | ||
<Demo /> | ||
</ErrorBoundary> | ||
); | ||
``` | ||
|
||
## Reference | ||
|
||
```js | ||
const dispatchError = useError(); | ||
``` | ||
|
||
- `dispatchError` — Callback of type `(err: Error) => void` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useState, useEffect, useCallback } from 'react'; | ||
|
||
const useError = (): ((err: Error) => void) => { | ||
const [error, setError] = useState<Error | null>(null); | ||
|
||
useEffect(() => { | ||
if (error) { | ||
throw error; | ||
} | ||
}, [error]); | ||
|
||
const dispatchError = useCallback((err: Error) => { | ||
setError(err); | ||
}, []); | ||
|
||
return dispatchError; | ||
}; | ||
|
||
export default useError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { storiesOf } from '@storybook/react'; | ||
import React from 'react'; | ||
import { useError } from '../src'; | ||
import ShowDocs from './util/ShowDocs'; | ||
|
||
class ErrorBoundary extends React.Component<{}, { hasError: boolean }> { | ||
constructor(props) { | ||
super(props); | ||
this.state = { hasError: false }; | ||
} | ||
|
||
static getDerivedStateFromError(error) { | ||
return { hasError: true }; | ||
} | ||
|
||
render() { | ||
if (this.state.hasError) { | ||
return ( | ||
<div> | ||
<h1>Something went wrong.</h1> | ||
<button onClick={() => this.setState({ hasError: false })}>Retry</button> | ||
</div> | ||
); | ||
} | ||
|
||
return this.props.children; | ||
} | ||
} | ||
|
||
const Demo = () => { | ||
const dispatchError = useError(); | ||
|
||
const clickHandler = () => { | ||
dispatchError(new Error('Some error!')); | ||
}; | ||
|
||
return <button onClick={clickHandler}>Click me to throw</button>; | ||
}; | ||
|
||
storiesOf('Side effects|useError', module) | ||
.add('Docs', () => <ShowDocs md={require('../docs/useLocalStorage.md')} />) | ||
.add('Demo', () => ( | ||
<ErrorBoundary> | ||
<Demo /> | ||
</ErrorBoundary> | ||
)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { useError } from '../src'; | ||
|
||
const setup = () => renderHook(() => useError()); | ||
|
||
beforeEach(() => { | ||
jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should throw an error on error dispatch', () => { | ||
const errorStr = 'some_error'; | ||
|
||
try { | ||
const { result } = setup(); | ||
|
||
act(() => { | ||
result.current(new Error(errorStr)); | ||
}); | ||
} catch (err) { | ||
expect(err.message).toEqual(errorStr); | ||
} | ||
}); |