when i collapese the folder and open again, all the opened folders in its children,…
- User
when i collapese the folder and open again, all the opened folders in its children, collapses
import { useState } from "react"
const Folder = ({ root }) => { const [expanded, setIsExpanded] = useState({}) if (root.isFolder) { return ( <div onClick={() => { setIsExpanded(prev => ({ ...prev, [root.name]: !prev[root.name] })) }}>📁{root.name} { expanded[root.name] && <div style={{ paddingLeft: '24px' }}> { root.children?.map(child => ) } }
</div > )} else { return ( 📄{root.name} ) } }
export default Folder
- Assistant
The behavior you're experiencing where all opened folders collapse when you reopen a parent folder is due to how you're managing the
expandedstate. The current implementation relies on folder names as keys in theexpandedstate object. This results in the state being lost when you close and reopen a parent folder because the keys are created only for the current level of folders.