Answer the question
In order to leave comments, you need to log in
What is the best way to organize a directory state with drop-down lists?
I have a test directory with themes. Themes are usually 20 or more. Clicking on a topic will open a list of subtopics.
<div
className="Topic"
data-subtopic-list="subtopicList1"
onClick={handleTopicClick}
>
{subtopicLists.subtopicList1 && (
<div className="Topic-SubtopicList">
...
</div>
)}
</div>
const [subtopicLists, setSubtopicLists] = useState({
subtopicList1: false,
subtopicList2: false,
subtopicList3: false,
subtopicList4: false,
subtopicList5: false,
subtopicList6: false,
subtopicList7: false,
subtopicList8: false,
subtopicList9: false,
subtopicList10: false,
subtopicList11: false,
subtopicList12: false,
subtopicList13: false,
subtopicList14: false,
subtopicList15: false,
subtopicList16: false,
subtopicList17: false,
subtopicList18: false,
subtopicList19: false,
extraSubtopicList1: false,
extraSubtopicList2: false,
extraSubtopicList3: false
});
const handleTopicClick = e => {
e.preventDefault();
const { subtopicList } = e.currentTarget.dataset;
setSubtopicLists({
...subtopicLists,
...{ [subTopicList]: !subtopicLists[subTopicList] }
});
};
Answer the question
In order to leave comments, you need to log in
1. Brutal hardcoding.
subtopicList1: false,
subtopicList2: false,
data-subtopic-list="subtopicList1"
onClick={handleTopicClick}
const handleTopicClick = subtopicList => {
setSubtopicLists({
...subtopicLists,
...{ [subTopicList]: !subtopicLists[subTopicList] }
});
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question