-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
5 changed files
with
79 additions
and
13 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,15 @@ | ||
import * as React from 'react'; | ||
import { render } from 'react-testing-library'; | ||
import UseEffectOnUpdate from './UseEffectOnUpdate'; | ||
|
||
test('should call props.fn only when inputs change', (): void => { | ||
const fn = jest.fn(); | ||
const { rerender } = render(<UseEffectOnUpdate fn={fn} inputs={[1]} />); | ||
expect(fn).toHaveBeenCalledTimes(0); | ||
|
||
rerender(<UseEffectOnUpdate fn={fn} inputs={[1]} />); | ||
expect(fn).toHaveBeenCalledTimes(0); | ||
|
||
rerender(<UseEffectOnUpdate fn={fn} inputs={[2]} />); | ||
expect(fn).toHaveBeenCalledTimes(1); | ||
}); |
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,38 @@ | ||
import * as React from 'react'; | ||
import UseEffect from './UseEffect'; | ||
import { EqualityFn, VoidFn } from './utils'; | ||
|
||
interface Props { | ||
fn: () => void | VoidFn; | ||
inputs?: any[]; // eslint-disable-line @typescript-eslint/no-explicit-any | ||
comparator?: EqualityFn; | ||
} | ||
|
||
interface State { | ||
mounted: boolean; | ||
} | ||
|
||
export default class UseEffectOnUpdate extends React.Component<Props, State> { | ||
state = { | ||
mounted: false | ||
}; | ||
|
||
callback = (): void => { | ||
if (this.state.mounted) { | ||
this.props.fn(); | ||
return; | ||
} | ||
|
||
this.setState({ mounted: true }); | ||
}; | ||
|
||
render(): JSX.Element { | ||
return ( | ||
<UseEffect | ||
fn={this.callback} | ||
inputs={this.props.inputs} | ||
comparator={this.props.comparator} | ||
/> | ||
); | ||
} | ||
} |
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