I'm new to reactjs and this may be a basic knowledge, but I cannot find any related information on the internet.
I have the following code that pops up a Modal window when you click some button (clicking the button results in history.push(url)
action, which pops up the modal window ):
const ProjectBoard = () => {
const match = useRouteMatch();
const history = useHistory();
const [filters, mergeFilters] = useMergeState(defaultFilters);
const [IssueCreateModalOpen, setIssueCreateModalOpen] = useState(false);
const [{ data, error, setLocalData }, fetchProject] = useApi.get('/project');
if (!data) return <PageLoader />;
if (error) return <PageError />;
const { project } = data;
const updateLocalProjectIssues = (issueId, updatedFields) => {
setLocalData(currentData => ({
project: {
...currentData.project,
issues: updateArrayItemById(currentData.project.issues, issueId, updatedFields),
},
}));
};
return (
<Fragment>
<Header/>
<Lists
project={project}
filters={filters}
updateLocalProjectIssues={updateLocalProjectIssues}
/>
<br/>
<Route
path={`${match.path}/issues/:issueId`}
render={routeProps => (
<Modal
isOpen // confusion 1: this variable is not defined anywhere!!
testid="modal:issue-details"
width={1040}
withCloseIcon={false}
onClose={()=>history.push(match.url)}
renderContent={modal => (
<IssueDetails
issueId={routeProps.match.params.issueId}
trigger={routeProps.location.state.trigger}
projectUsers={project.users}
fetchProject={fetchProject}
updateLocalProjectIssues={updateLocalProjectIssues}
modalClose={modal.close}
/>
)}
/>
)}
/>
</Fragment>
);
}
export default ProjectBoard;
Below is where the Modal
component is defined:
import React, { Fragment, useState, useRef, useEffect, useCallback } from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import useOnOutsideClick from '../../hooks/onOutsideClick';
import useOnEscapeKeyDown from '../../hooks/onEscapeKeyDown';
const propTypes = {
className: PropTypes.string,
testid: PropTypes.string,
variant: PropTypes.oneOf(['center', 'aside']),
width: PropTypes.number,
withCloseIcon: PropTypes.bool,
isOpen: PropTypes.bool,
onClose: PropTypes.func,
renderLink: PropTypes.func,
renderContent: PropTypes.func.isRequired,
};
const defaultProps = {
className: undefined,
testid: 'modal',
variant: 'center',
width: 600,
withCloseIcon: true,
isOpen: undefined,
onClose: () => {},
renderLink: () => {},
};
const Modal = ({
className,
testid,
variant,
width,
withCloseIcon,
isOpen: propsIsOpen, // confusion 3: what does it mean, x:y ?
onClose: tellParentToClose,
renderLink,
renderContent,
}) => {
console.log('---- propsIsOpen: ', propsIsOpen, typeof(propsIsOpen))
const [stateIsOpen, setStateOpen] = useState(false);
const isControlled = typeof propsIsOpen === 'boolean';
const isOpen = isControlled ? propsIsOpen : stateIsOpen; // confusion 2: if isOpen is defined here, why even bother to pass a prop named as isOpen ??
const $modalRef = useRef();
const $clickableOverlayRef = useRef();
const closeModal = useCallback(() => {
if (!isControlled) {
setStateOpen(false);
} else {
tellParentToClose();
}
}, [isControlled, tellParentToClose]);
useOnOutsideClick($modalRef, isOpen, closeModal, $clickableOverlayRef);
useOnEscapeKeyDown(isOpen, closeModal);
useEffect(() => {
console.log('Modal renderContent: ', renderContent)
document.body.style.overflow = 'hidden';
return () => {
document.body.style.overflow = 'visible';
};
}, [isOpen]);
return (
<Fragment>
{!isControlled && renderLink({ open: () => setStateOpen(true) })}
{isOpen &&
ReactDOM.createPortal(
<ScrollOverlay>
<ClickableOverlay variant={variant} ref={$clickableOverlayRef}>
... some code ...
</ClickableOverlay>
</ScrollOverlay>,
$root,
)}
</Fragment>
);
};
const $root = document.getElementById('root');
Modal.propTypes = propTypes;
Modal.defaultProps = defaultProps;
export default Modal;
The above code segments works for sure. I just cannot get my head around with:
confusion 1:the variable isOpen
is not defined anywhere before it is pass as a prop to the Modal
component
confusion 2: isOpen
is defined within the Modal
component as a "fresh new variable", why even bother to pass a prop named as isOpen
to the Modal
component in the first place??
confusion 3: what does x:y
mean in the input for a component? i.e. isOpen: propsIsOpen,