-
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
11 changed files
with
894 additions
and
101 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"plugins": ["transform-object-rest-spread"], | ||
"presets": ["es2015", "react"] | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
/coverage/* | ||
/lib/* |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2017 Drew Keller | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,115 @@ | ||
# react-component-update | ||
|
||
[![Build Status](https://travis-ci.org/wimpyprogrammer/react-component-update.svg?branch=master)](https://travis-ci.org/wimpyprogrammer/react-component-update) | ||
[![codecov](https://codecov.io/gh/wimpyprogrammer/react-component-update/branch/master/graph/badge.svg)](https://codecov.io/gh/wimpyprogrammer/react-component-update) | ||
|
||
Adds convenience lifecycle events to your React components. | ||
|
||
- `componentWillMountOrReceiveProps(nextProps)` - Combines the [`componentWillMount()`](https://facebook.github.io/react/docs/react-component.html#componentwillmount) and [`componentWillReceiveProps(nextProps)`](https://facebook.github.io/react/docs/react-component.html#componentwillreceiveprops) events. This allows you to consolidate all pre-`render()` logic. | ||
|
||
- `componentDidMountOrUpdate(prevProps, prevState)` - Combines the [`componentDidMount()`](https://facebook.github.io/react/docs/react-component.html#componentdidmount) and [`componentDidUpdate(prevProps, prevState)`](https://facebook.github.io/react/docs/react-component.html#componentdidupdate) events. This allows you to consolidate all post-`render()` logic. | ||
|
||
## Installation | ||
|
||
Published on `npm` as [`react-component-update`](https://www.npmjs.com/package/react-component-update). | ||
|
||
npm users: | ||
``` | ||
npm install --save react-component-update | ||
``` | ||
|
||
yarn users: | ||
``` | ||
yarn add react-component-update | ||
``` | ||
|
||
`react-component-update` does not include its own version of React. It will use whatever version is already installed in your project. | ||
|
||
## Usage | ||
|
||
To extend React's `Component` class: | ||
|
||
```js | ||
import React from 'react'; | ||
import { Component } from 'react-component-update'; | ||
|
||
class MyReactComponent extends Component { | ||
componentWillMountOrReceiveProps(nextProps) { | ||
// Code that runs before every render(). For example, check that the data | ||
// used by this component has already loaded, otherwise trigger an AJAX | ||
// request for it. nextProps contains the props that render() will receive. | ||
} | ||
|
||
componentDidMountOrUpdate(prevProps, prevState) { | ||
// Code that runs after every render(). For example, inspect the latest DOM | ||
// to get the height of the rendered elements. prevProps and prevState | ||
// contain the props and state that render() will receive. | ||
} | ||
|
||
render() { | ||
return <div />; | ||
} | ||
} | ||
``` | ||
|
||
Or to extend React's `PureComponent` class (available in React v15.3.0+): | ||
```js | ||
import { PureComponent } from 'react-component-update'; | ||
``` | ||
|
||
For compatibility with [`create-react-class`](https://www.npmjs.com/package/create-react-class), use the `withEvents()` higher-order component. | ||
|
||
```js | ||
import createReactClass from 'create-react-class'; | ||
import { withEvents } from 'react-component-update'; | ||
|
||
const MyReactComponent = createReactClass(withEvents({ | ||
componentWillMountOrReceiveProps: function(nextProps) { | ||
// Code that runs before every render(). | ||
}, | ||
|
||
componentDidMountOrUpdate: function(prevProps, prevState) { | ||
// Code that runs after every render(). | ||
}, | ||
|
||
render: function() { | ||
return <div />; | ||
} | ||
})); | ||
``` | ||
|
||
## Mixing with your own lifecycle events | ||
|
||
`react-component-update` implements four lifecycle events of the React base classes: | ||
- `componentWillMount()` | ||
- `componentDidMount()` | ||
- `componentWillReceiveProps()` | ||
- `componentDidUpdate()` | ||
|
||
If you extend `Component` or `PureComponent` from `react-component-update` and you also implement these events in your component, you will need to call the corresponding `super()` method like so: | ||
|
||
```js | ||
componentWillMount() { | ||
super.componentWillMount(); | ||
} | ||
|
||
componentDidMount() { | ||
super.componentDidMount(); | ||
} | ||
|
||
componentWillReceiveProps(nextProps) { | ||
super.componentWillReceiveProps(nextProps); | ||
} | ||
|
||
componentDidUpdate(prevProps, prevState) { | ||
super.componentDidUpdate(prevProps, prevState); | ||
} | ||
``` | ||
|
||
The `super()` method can be called anywhere in your function to suit your needs. | ||
|
||
If you use the `withEvents()` higher-order component, you do not need to add any extra code to your events. The new event (ex. `componentDidMountOrUpdate()`) will always run after the related built-in event (ex. `componentDidUpdate()`). | ||
|
||
## License | ||
|
||
[MIT](/LICENSE.md) |
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
Oops, something went wrong.