Async props update HoC for React Component
$ npm install async-update-props
import { compose } from 'redux';
import { connect } from 'react-redux';
import asyncUpdateProps from 'async-update-props';
import Page from '../components/Page';
import { fetchDataRequest } from '../actions';
const mapStateToProps = (state) => ({
...
});
const updater = (props) => {
props.fetchDataRequest();
};
const shouldUpdateWhenReceiveProps = () => false;
export default compose(
connect(mapStateToProps, {
fetchDataRequest,
}),
asyncUpdateProps(updater, shouldUpdateWhenReceiveProps)
)(Page);
If updater returns a promise, setState will be called with resolved value automatically:
import asyncUpdateProps from 'async-update-props';
import Page from '../components/Page';
import * as apis from '../apis';
const updater = () => {
return apis.fetchDataRequest();
};
const shouldUpdateWhenReceiveProps = () => false;
export default asyncUpdateProps(updater, shouldUpdateWhenReceiveProps)(Page);
Required
Type: func
Optional
Type: func
Default: () => true
MIT © Yoctol