Security News
New Proposed CISA Mandate Would Require Critical Infrastructure to Report Ransom Payments Within 24 Hours
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
react-timeout
Advanced tools
HOC for React and React Native providing versions of setTimeout etc. that cancels when unmounted.
Weekly downloads
Readme
Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op.
Warning: Can only update a mounted or mounting component. This usually means you called setState, replaceState, or forceUpdate on an unmounted component. This is a no-op.
Seeing a lot of the above? If so this might be useful!
React Timeout is a higher order component for React and React Native providing the wrapped component with safe versions of
Set | Clear |
---|---|
setTimeout | clearTimeout |
setInterval | clearInterval |
setImmediate | clearImmediate |
requestAnimationFrame | cancelAnimationFrame |
When the wrapped component is unmounted, any lingering timers will be canceled automatically.
npm install --save react-timeout
React | React Timeout |
---|---|
16.3 + | 2+ |
<16.3 | 1.x |
import ReactTimeout from 'react-timeout'
The component simulates a light switch. It has a state on
which is true
or false
. When the button is clicked it waits 5000ms
before switching the on
state.
import React from 'react'
import ReactTimeout from 'react-timeout'
class LightSwitchExample extends React.Component {
state = {
on: false
}
toggle = () => {
this.setState({ on: !this.state.on })
}
handleClick = (e) => {
this.props.setTimeout(this.toggle, 5000) // call the `toggle` function after 5000ms
}
render () {
return (
<div style={{ backgroundColor: (this.state.on ? 'yellow' : 'gray') }}>
<button onClick={this.handleClick}>Click me!</button>
</div>
)
}
}
export default ReactTimeout(LightSwitchExample)
If the component is unmounted before the 5000ms
is up, the timeout is canceled by ReactTimeout
.
Had we just called the regular old setTimeout
, the callback toggle
would still fire and try setting the state of an unmounted component.
const Example = (props) => {
return (
<button
onClick={() => props.setTimeout(..)}>Click me!</button>
)
}
export default ReactTimeout(Example)
@ReactTimeout
class Example extends React.Component {
render () {
return (
<button
onClick={() => this.props.setTimeout(..)}>Click me!</button>
)
}
}
You can access the wrapped instance using React.createRef
from version 2+
.
If you're using version 1.x
you can access the wrapped instance with component.getWrappedInstance()
.
The timer mixin recommended by the react-native docs.
Only supported by version 1.x
.
If you're using a version of React Native that is 0.17
or below you have to import from the /native
namespace.
import ReactTimeout from 'react-timeout/native' // only for react-native 0.17 or below
FAQs
HOC for React and React Native providing versions of setTimeout etc. that cancels when unmounted.
The npm package react-timeout receives a total of 9,748 weekly downloads. As such, react-timeout popularity was classified as popular.
We found that react-timeout demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
Security News
Redis is no longer OSS, breaking its explicit commitment to remain under the BSD 3-Clause License forever. This has angered contributors who are now working to fork the software.
Product
Socket AI now enables 'AI detected potential malware' alerts by default, ensuring users benefit from AI-powered state-of-the-art malware detection without needing to opt-in.