react-container-helper
Help to generate react container component easily and functionally. Powered by Higher-Order Components.
Inspire by
Feature
Full Example
You can see the simple example on example
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
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. getState
and getProps
can get the current container's value - [
componentDidMount({ setState, getState, getProps })
](Function): Will be called on container did mount. 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. 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 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. 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. 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. getState
and getProps
can get the current container's value. Help you to clearup container.
Contribute
- Fork it.
- Create your feature-branch
git checkout -b your-new-feature-branch
- Commit your change
git commit -am 'Add new feature'
- Push to the branch
git push origin your-new-feature-branch
- 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.