0
I have a parent component that opens a modal based on the value of this variable:
const [openFullScreenModal, setOpenFullScreenModal] = useState(false)
I have a button in this component that arrow the value of this variable to true
:
setOpenFullScreenModal(true)
In my template, I have a modal that opens when the value of this variable is true:
{ openFullScreenModal === true ?
<ModalFullScreen
FormComponent={UsersFormPermissionsGroupPermissions}
title="Permissões do grupo"
onCloseModal={setOpenFullScreenModal}
open={openFullScreenModal}
/>
:
<> </>
}
This is my Modalfullscreen Component:
const ModalFullScreen = (props) => {
const { title, FormComponent, open, onCloseModal } = props
const classes = modalFullScreenStyles()
const handleClose = () => {
onCloseModal(false)
}
return (
<div>
<Dialog fullScreen open={open} onClose={handleClose}>
<AppBar className={classes.appBar}>
<Toolbar>
<Typography variant="h6" className={classes.title}>
{title}
</Typography>
<IconButton edge="start" color="inherit" onClick={handleClose} aria-label="fechar">
<Typography variant="h6" className={classes.title}>
Cancelar
</Typography>
</IconButton>
</Toolbar>
</AppBar>
<FormComponent></FormComponent>
</Dialog>
</div>
)
In my UsersFormPermissionsGroupPermissions component I use a useEffect()
to fetch data from an api:
useEffect(() => {
console.log('mounted again')
dispatch(groupPermissionsActions.getPermissionsGroupById(idGroupCompany))
})
I realized that my api was being called twice, so I put this console.log()
and confirmed that my component is being re-rendered twice when I click on the button of my parent component to open that modal, my modal opens and the component I am passing to the modal is rendered twice.
'Cause this is happening?
When that
UsersFormPermissionsGroupPermissions
is created and then it automatically updates, the related component also updates, Maybe it is this and your rendering observation is very pertinent, as we do not have the total code gets complicated to say but, face I would like this, in the Father component of the two components I rescued the information and byprops
passed to this other component so the Father is responsible for sending only information to the children.– novic
It is not that it is rendering two must, it creates the component and then updates the component with the asynchronous process if it could already send the information to it before opening
– novic
@Virgilionovic actually, after I do the Dispatch he renders the component again. Can you provide an example of how I can pass a property to the component my modal will render? I tried something like: <Modalfullscreen Formcomponent={Usersformpermissionsgrouppermissions propriedadePassar="test"} but got "}" expected
– veroneseComS
@Virgilionovic I accidentally forgot to put the [] after the useEffect, so after any action he was rendering my component again. I could easily fix with: useEffect(() => { Dispatch(groupPermissionsActions.getPermissionsGroupById(idGroupCompany)) }, [])
– veroneseComS