Use redux hooks on alert components

This commit is contained in:
Grégoire Delattre 2020-04-03 16:02:39 +02:00
parent ea62b1c6ec
commit 6cfee5ea74

View File

@ -1,35 +1,27 @@
import React from "react"; import React from "react";
import PropTypes from "prop-types";
import SweetAlert from "react-bootstrap-sweetalert"; import SweetAlert from "react-bootstrap-sweetalert";
import { connect } from "react-redux"; import { useSelector, useDispatch } from "react-redux";
import { dismissAlert } from "../../actions/alerts"; import { dismissAlert } from "../../actions/alerts";
const mapStateToProps = (state) => ({ const Alert = () => {
show: state.alerts.get("show"), const dispatch = useDispatch();
title: state.alerts.get("message"),
type: state.alerts.get("type"),
});
const mapDispatchToProps = { dismissAlert };
const Alert = (props) => { const show = useSelector((state) => state.alerts.get("show"));
if (!props.show) { const title = useSelector((state) => state.alerts.get("message"));
const type = useSelector((state) => state.alerts.get("type"));
if (!show) {
return null; return null;
} }
return ( return (
<SweetAlert <SweetAlert
type={props.type} type={type}
title={props.title} title={title}
onConfirm={props.dismissAlert} onConfirm={() => dispatch(dismissAlert())}
/> />
); );
}; };
Alert.propTypes = {
show: PropTypes.bool.isRequired,
title: PropTypes.string.isRequired,
dismissAlert: PropTypes.func.isRequired,
type: PropTypes.string,
};
export default connect(mapStateToProps, mapDispatchToProps)(Alert); export default Alert;