I have the following React component. The top-level component is <App />
.
Let's say the <div id="keyDiv">
element were to get deleted (line 10 of the code snippet below). What triggers this isn't really important to me - maybe another part of the App did it, or maybe a user did it via Chrome Devtools. Either way, deleting <div id="keyDiv">
will effectively kill all the children of <Container>
(at line 9).
Is there any way to re-render the children of <Container>
, so that those children will get re-generated? (particularly <div id="keyDiv"><Modal/></div>
)
I have tried updating the state of Container, but this does not restore its children.
import React, {FC, useState} from 'react';
const App : FC = () => {
const styles = {
border:'1px solid black',
backgroundColor: 'palegoldenrod',
padding:'10px'
}
return (
<Container>
<div style={styles} id="keyDiv">
<Modal />
</div>
</Container>
)
}
const Container : FC = ({children}) => {
const styles = {
border:'1px solid black',
padding:'10px'
}
const [label, setLabel] = useState('This is my label');
return (
<div>
<div style={styles}>
{label}
{children}
</div>
<button onClick={() => {setLabel(`This is the date/time: ${new Date().toLocaleTimeString()}`)}}>Click me</button>
</div>
)
}
const Modal : FC = () => {
const styles = {
border:'1px solid black',
padding:'10px',
backgroundColor: 'lightgreen'
}
return (
<div style={styles}>
This should be the modal
</div>
)
}