From cde94ec11bc0efb35a67ad527078b2742d67d236 Mon Sep 17 00:00:00 2001 From: plougsgaard Date: Sat, 16 Apr 2016 02:38:58 +0200 Subject: [PATCH] Prepare major release. --- .babelrc | 2 +- README.md | 89 +++++++++++++++++++++++++++++++++++++++------------- package.json | 18 ++++++----- 3 files changed, 78 insertions(+), 31 deletions(-) diff --git a/.babelrc b/.babelrc index 9b7d435..0e3e3f8 100644 --- a/.babelrc +++ b/.babelrc @@ -1,3 +1,3 @@ { - "presets": ["es2015", "stage-0", "react"] + "presets": ["es2015", "stage-1", "react"] } diff --git a/README.md b/README.md index a3c08b9..50f7b5f 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,11 @@ [![travis build](https://img.shields.io/travis/plougsgaard/react-timeout.svg)](https://travis-ci.org/plougsgaard/react-timeout) [![npm version](https://badge.fury.io/js/react-timeout.svg)](https://badge.fury.io/js/react-timeout) -A component wrapper providing **safe-to-use-with-react** versions of +> 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. + +Seeing a lot of the above? If so this might be useful! + +React Timeout is a higher order component for [React](https://github.com/facebook/react) and [React Native](https://github.com/facebook/react-native) providing the wrapped component with **safe** versions of Set | Clear ------------------------|------------------------ @@ -11,9 +15,9 @@ Set | Clear `setImmediate` | `clearImmediate` `requestAnimationFrame` | `cancelAnimationFrame` -When the component is *unmounted* the wrapper calls the **Clear** functions for you. +When the wrapped component is *unmounted*, any lingering timers will be canceled automatically. -## Installation +# Installation `npm install --save react-timeout` @@ -33,41 +37,82 @@ For previous versions, import it like so: import ReactTimeout from 'react-timeout/native' ``` -## Usage +# Examples + +## React "Classic" (verbose) -Apply **ReactTimeout** using composition +This simulates a light switch that takes `5000ms` to switch between `on` and `!on`. ```javascript -class Example extends Component { .. } -const WithReactTimeout = ReactTimeout(Example) +import ReactTimeout from 'react-timeout' + +var Example = React.createClass({ + toggleOn: function () { + this.setState({ on: !this.state.on }) + }, + handleClick: function (e) { + this.props.setTimeout(this.toggleOn, 5000) + }, + render: function () { + return ( + + ) + } +}) + +export default ReactTimeout(Example) ``` -or an annotation (not recommended) +Had we just called the regular old `setTimeout` and unmounted the component, the callback would still fire and try setting the state of an unmounted component. However since we use `ReactTimeout` the `this.props.setTimeout` will get canceled the moment the component unmounts. + +## ES6 Classes ```javascript -@ReactTimeout -class WithReactTimeout extends Component { .. } +class Example extends Component { + render() { + return ( + + ) + } +} +export default ReactTimeout(Example) +``` + +## Functional Stateless Components + +```javascript +const Example = ({ setTimeout }) => ({ + +}) +export default ReactTimeout(Example) ``` -Invoke a safe `setTimeout` from within the component. +## With ES7 Annotations ```javascript -const { setTimeout } = this.props.reactTimeout -const id = setTimeout(() => { - console.log(`The callback with ${id} fired!`) -}, 5000) +@ReactTimeout +class Example extends Component { .. } +``` + +```javascript +@ReactTimeout +var Example = React.createClass({ .. }) ``` -The callback function will be cleared if the component unmounts before the `5000ms` elapse. +# Something similar -## Example +## [react-timer-mixin](https://github.com/reactjs/react-timer-mixin) -A full example is available in `example/src/example.js`. +The timer mixin recommended by the [react-native](https://github.com/facebook/react-native) docs. -To run the example, clone the repository and run `npm install && npm start` in the `example/` folder. +# Changes -## Similar +## Changes in ^1.0.0 -### [react-timer-mixin](https://github.com/reactjs/react-timer-mixin) +Since the major version changed from `0` to `1` the only breaking change is dropping the `reactTimeout` namespace. -The timer mixin recommended by the [react-native](https://github.com/facebook/react-native) `README.md`. +For example: `this.props.reactTimeout.setTimeout` becomes `this.props.setTimeout`. diff --git a/package.json b/package.json index a6fa0ab..ff77515 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "react-timeout", - "version": "0.16.1", - "description": "Component wrapper for setTimeout et al that cleans up after itself.", + "version": "1.0.0", + "description": "HOC for React and React Native providing versions of setTimeout etc. that cancels when unmounted.", "homepage": "https://github.com/plougsgaard/react-timeout", "author": "plougsgaard", "repository": { @@ -12,7 +12,9 @@ "react", "reactjs", "native", - "timer" + "timer", + "timeout", + "hoc" ], "license": "MIT", "bugs": { @@ -30,14 +32,14 @@ "babel-core": "^6.2.0", "babel-preset-es2015": "^6.1.18", "babel-preset-react": "^6.1.18", - "babel-preset-stage-0": "^6.1.18", + "babel-preset-stage-1": "^6.1.18", "chai": "^3.4.1", "chai-immutable": "^1.5.3", - "immutable": "^3.7.5", + "immutable": "^3.8.0", "jsdom": "^7.0.2", "mocha": "^2.3.4", - "react": "^0.14.3", - "react-addons-test-utils": "^0.14.3", - "react-dom": "^0.14.3" + "react": "^0.14.8", + "react-addons-test-utils": "^0.14.8", + "react-dom": "^0.14.8" } }