Skip to content

Commit

Permalink
Merge branch 'release/1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
wimpyprogrammer committed Sep 8, 2017
2 parents ca24f86 + f3b7747 commit 7af609d
Show file tree
Hide file tree
Showing 11 changed files with 894 additions and 101 deletions.
1 change: 1 addition & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"plugins": ["transform-object-rest-spread"],
"presets": ["es2015", "react"]
}
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ end_of_line = lf
insert_final_newline = true
indent_style = tab

[package.json]
[{.babelrc,package.json}]
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/coverage/*
/lib/*
21 changes: 21 additions & 0 deletions LICENSE
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.
115 changes: 115 additions & 0 deletions README.md
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)
17 changes: 13 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-component-update",
"version": "1.0.0-alpha",
"version": "1.0.0",
"description": "Extends the native React Component to streamline updates",
"main": "lib/index.js",
"scripts": {
Expand All @@ -17,18 +17,20 @@
"url": "git+https://github.com/wimpyprogrammer/react-component-update.git"
},
"author": "Drew Keller <drew@wimpyprogrammer.com>",
"license": "ISC",
"license": "MIT",
"bugs": {
"url": "https://github.com/wimpyprogrammer/react-component-update/issues"
},
"homepage": "https://github.com/wimpyprogrammer/react-component-update#readme",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-jest": "^20.0.3",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"chai": "^4.0.2",
"codecov": "^2.3.0",
"create-react-class": "^15.6.0",
"dirty-chai": "^2.0.0",
"enzyme": "^2.8.2",
"eslint": "^3.19.0",
Expand All @@ -44,13 +46,20 @@
"react-dom": "*",
"react-test-renderer": "*",
"rimraf": "^2.6.1",
"sinon": "^2.3.4"
"sinon": "^3.2.1",
"sinon-chai": "^2.13.0"
},
"peerDependencies": {
"react": "*"
},
"jest": {
"coverageDirectory": "./coverage/",
"collectCoverage": true
"collectCoverage": true,
"testMatch": [
"**/src/*.spec.js?(x)"
]
},
"dependencies": {
"lodash.wrap": "^4.1.1"
}
}
Loading

0 comments on commit 7af609d

Please sign in to comment.