与其通过“将” redux“连接”到组件的仪式(设置连接,调度等)来代替,不如通过某种方式让React“ Component”仅仅是变相的redux动作派遣者? >
function AppModal({
show,
content,
title,
footer,
onRequestClose,
callTriggerModal
}) {
callTriggerModal({
show,
content,
title,
footer,
onRequestClose,
});
return null;
}
function mapDispatchToProps(dispatch) {
return {
callTriggerModal(options) {
dispatch(
triggerModal(options)
);
}
};
}
export default connect(() => ({}), mapDispatchToProps)(AppModal);
从外面叫它...
<AppModal show="true" content={<div>234</div>} ...>
这不起作用,因为redux调用本身会导致无限循环。但是要勾勒出这个想法。这有可能实现吗?
EDIT 看起来与UseEffect一样,这可能有效。
export function AppModal({
show,
content,
title,
footer,
onRequestClose,
callTriggerModal
}) {
useEffect(() => {
callTriggerModal({
show,
content,
title,
footer,
onRequestClose,
});
}, []);
return null;
}
function mapDispatchToProps(dispatch) {
return {
callTriggerModal(options) {
dispatch(
triggerModal(options)
);
}
};
}
export default connect(() => ({}), mapDispatchToProps)(AppModal);