# react-container-helper

> Help to generate react container component easily and functionally

Latest version **0.3.3** (published 2017-10-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-container-helper
pnpm add react-container-helper
yarn add react-container-helper
bun add react-container-helper
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.3 |
| Published | 2017-10-06 |
| First published | 2016-11-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Maintainers | jessy1092 |
| Keywords | react, react container, react-container, react container component, container component |

## Links

- npm: https://www.npmjs.com/package/react-container-helper
- Repository: https://github.com/jessy1092/react-container-helper
- Homepage: https://github.com/jessy1092/react-container-helper#readme
- Issues: https://github.com/jessy1092/react-container-helper/issues
- npm.io page: https://npm.io/package/react-container-helper

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.3.3 (latest) — 2017-10-06
- 0.3.2 — 2017-05-16
- 0.3.1 — 2017-04-28
- 0.3.0 — 2017-04-15
- 0.2.3 — 2017-04-15
- 0.2.2 — 2016-11-13
- 0.2.1 — 2016-11-13
- 0.2.0 — 2016-11-12
- 0.1.0 — 2016-11-09

## README

react-container-helper
=============
[![npm][npm-image]][npm-url] [![Build Status][travis-ci-image]][travis-ci-url] [![Dependency Status][david-dm-image]][david-dm-url] [![Coverage Status][coverage-status-image]][coverage-status-url]

Help to generate react container component easily and functionally. Powered by Higher-Order Components.

Inspire by

- [react-redux](https://github.com/reactjs/react-redux)
- [Presentational and Container Components -- Dan Abramov](https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0#.39eod2kgj).
- [Mixins Are Dead. Long Live Composition -- Dan Abramov](https://medium.com/@dan_abramov/mixins-are-dead-long-live-higher-order-components-94a0d2f9e750#.xj7geuov2).


## Feature

- [x] Use `initState` to set container's initial state
- [x] Use `mapSetStateToProps` to map container's state, props or setState to component's properties
- [x] Support container's life cycle method
- [ ] Support purecomponent.


## Full Example

You can see the simple example on [example](./example)

```js
import {contain} from 'react-container-helper';

const Button = ({ toggle, handleClick }) => (
  <button onClick={handleClick}>
    {toggle ? 'true' : 'false'}
  </button>
);

const initState = () => ({
  toggle: false,
});

const mapSetStateToProps = ({ toggle }, _, setState) => ({
  toggle,
  handleClick: () => setState({ toggle: !toggle }),
});

const ToggleButton = contain(initState, mapSetStateToProps)(Button);

ReactDOM.render(
  <ToggleButton />,
  document.getElementById('content')
);
```

Origin
```js
import React from 'react';

const Button = ({toggle, handleClick}) => (
  <button onClick={handleClick}>
    {toggle ? "true" : "false"}
  </button>
);

class ToggleButton extends React.Component {

  constructor(props) {
    super(props);
    this.state = {toggle: false};

    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
    this.setState({toggle: !this.state.toggle});
  }

  render() {
    return (
      <Button handleClick={this.handleClick} toggle={this.state.toggle}/>
    );
  }
}

ReactDOM.render(
  <ToggleButton />,
  document.getElementById('content')
);
```


## API

### contain(initState, mapSetStateToProps, setLifecycle)

#### Arguments

- [`initState(props)`]\(*Function*): Use to set container initial state
- [`mapSetStateToProps(state, props, setState)`]\(*Function*): Help to map container's state, props or setState to component's properties
- [`setLifecycle()`]\(*Function*): Help to set container's lifecycle. Return the lifecycle object

#### Container's Lifecycle

Please use the `setLifecycle` to set and return the lifecycle object which define the lifecycle function.

- [`componentWillMount({ getState, getProps })`]\(*Function*): Will be called on [container will mount](https://facebook.github.io/react/docs/react-component.html#componentwillmount). `getState` and `getProps` can get the current container's value
- [`componentDidMount({ setState, getState, getProps })`]\(*Function*): Will be called on [container did mount](https://facebook.github.io/react/docs/react-component.html#componentdidmount). `getState` and `getProps` can get the current container's value. You can use `setState` to change the container's state
- [`componentWillReceiveProps(nextProps, { setState, getState, getProps })`]\(*Function*): Will be called on [container will receive properties](https://facebook.github.io/react/docs/react-component.html#componentwillreceiveprops). `nextProps` is the next properties. `getState` and `getProps` can get the current container's value. You can use `setState` to change the container's state
- [`shouldComponentUpdate(nextProps, nextState, { getState, getProps })`]\(*Function*): Use to check [container should update](https://facebook.github.io/react/docs/react-component.html#shouldcomponentupdate) or not. `nextProps` and `nextState` is the next container's value. `getState` and `getProps` can get the current container's value.
- [`componentWillUpdate(nextProps, nextState, { getState, getProps })`]\(*Function*): Will be called on [container will update](https://facebook.github.io/react/docs/react-component.html#componentwillupdate). `nextProps` and `nextState` is the next container's value. `getState` and `getProps` can get the current container's value.
- [`componentDidUpdate(prevProps, prevState, { setState, getState, getProps })`]\(*Function*): Will be called on [container did update](https://facebook.github.io/react/docs/react-component.html#componentdidupdate). `prevProps` and `prevState` is the preview container's value. `getState` and `getProps` can get the current container's value. You can use `setState` to change the container's state
- [`componentWillUnmount({ getState, getProps })`]\(*Function*): Will be called on [container will unmount](https://facebook.github.io/react/docs/react-component.html#componentwillunmount). `getState` and `getProps` can get the current container's value. Help you to clearup container.

## Contribute
[![devDependency Status][david-dm-dev-image]][david-dm-dev-url]

1. Fork it.
2. Create your feature-branch `git checkout -b your-new-feature-branch`
3. Commit your change `git commit -am 'Add new feature'`
4. Push to the branch `git push origin your-new-feature-branch`
5. Create new Pull Request with `develop` branch

## License

The MIT License (MIT)

Copyright (c) 2016-2017 Lee  < jessy1092@gmail.com >

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.


[npm-image]: https://img.shields.io/npm/v/react-container-helper.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/react-container-helper

[travis-ci-image]: https://img.shields.io/travis/jessy1092/react-container-helper.svg?style=flat-square
[travis-ci-url]: https://travis-ci.org/jessy1092/react-container-helper

[david-dm-image]: https://img.shields.io/david/jessy1092/react-container-helper.svg?style=flat-square
[david-dm-url]: https://david-dm.org/jessy1092/react-container-helper
[david-dm-dev-image]: https://img.shields.io/david/dev/jessy1092/react-container-helper.svg?style=flat-square
[david-dm-dev-url]: https://david-dm.org/jessy1092/react-container-helper#info=devDependencies

[coverage-status-image]: https://img.shields.io/coveralls/jessy1092/react-container-helper.svg?style=flat-square
[coverage-status-url]: https://coveralls.io/r/jessy1092/react-container-helper

---
_Source: https://npm.io/package/react-container-helper · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
