每次收到即将到来的新道具时,我都想更改本地状态。为此,我使用库中的lifecycle
HOC Recompose
。但这并不像我想的那么简单。我要么永远设置该值,要么在使用回调时发生堆栈溢出。
import { connect } from 'react-redux';
import { withState, withProps, lifecycle, compose } from 'recompose';
import { selectors, testDelete, testSubscribe } from 'ducks/entities/tests';
import { Tests } from 'components/tests/all-tests';
export default compose(
connect(selectors.tests, {
onTestDelete: testDelete,
onTestSubscribe: testSubscribe,
}),
withState('isDeleteModalOpen', 'setDeleteModalShow', false),
withState('idToDelete', 'setIdToDelete', 0),
withProps(
({
tests,
idToDelete,
setIdToDelete,
setDeleteModalShow,
onTestDelete,
onTestSubscribe,
}) => ({
tests: tests.map((t) =>
t.merge({
onDeleteModalShow: () => {
setDeleteModalShow(true);
setIdToDelete(t.get('id'));
},
onSubscribe: () => onTestSubscribe(t.get('id')),
}),
),
onDeleteModalHide: () => setDeleteModalShow(false),
onDelete: () => onTestDelete(idToDelete),
}),
),
lifecycle({
componentWillReceiveProps({ setDeleteModalShow }) {
setDeleteModalShow(false); // Not workding "Maximum update depth exceeded"
this.setState({
isDeleteModalOpen: false,
}); // now working
},
}),
)(Tests);