I want to keep open my sidemenu but i don't know how to do.
const Dep2Handler = (e) => {
const classOnTarget = e.target.parentElement;
const onRemoveTarget = classOnTarget.parentElement.children;
if (classOnTarget.classList.contains("on")) {
classOnTarget.classList.remove("on");
}
else {
for (let i = 0; i < onRemoveTarget.length; i++) {
onRemoveTarget[i].classList.remove("on");
}
classOnTarget.classList.add("on");
}
};
/* useLayoutEffect(()=>{
},[sideNaviPos]) */
const Dep3Handler = (e,props) => {
const classOnTarget = e.target;
const onRemoveTarget = classOnTarget.parentElement.children;
classOnTarget.classList.add("on");
};
--------------------------------------------------------------------
<ol>
<li>
<p onClick={Dep2Handler} sidebar={sidebar}></p>
<ol className="has_dep3">
<li onClick={Dep3Handler} value="space"><button className="btn" onClick={(e)=>history("/info")}></button></li>
<li onClick={Dep3Handler} value="equip"><button className="btn" onClick={(e)=>history("/info/info/equipinfo")}></button></li>
<li onClick={Dep3Handler} value="work"><button className="btn" onClick={(e)=>history("/info/info/workerinfo")}></button></li>
</ol>
</li>
<li>
<p onClick={Dep2Handler}></p>
<ol className="has_dep3">
<li onClick={Dep3Handler}><button onClick={(e)=>history("/admin/info/greetings")}></button></li>
<li onClick={Dep3Handler}><button onClick={(e)=>history("/admin/info/vision")}></button></li>
<li onClick={Dep3Handler}><button onClick={(e)=>history("/admin/info/organization")}></button></li>
<li onClick={Dep3Handler}><button onClick={(e)=>history("/admin/info/partner")}></button></li>
</ol>
</li>
<li>
<p onClick={(e)=>{Dep2Handler(e);history("/info/way")}}></p>
</li>
<li>
<p onClick={(e)=>{Dep2Handler(e);history("/info/faq")}}></p>
</li>
</ol>
if i click children of first li menu, keeping open then click another one the previous will be close and clicked menu open. Is it possible without use redux?
There is quite a bit to unpack here so I apologize in advance if this post ends up longer than you desired.
First off, and to address your question about Redux
Most definitely. Lots of people used React before redux was a thing and for some apps even today can get away without redux (using the Context API is an option).
Before just showing you to code let me comment on some of your existing code
by doing this you are effectively using the DOM's classList to hold your state rather than React. While there are cases that something like that might be needed (e.g. you are introducing some react components into an existing codebase) I don't think that's the case here and we should change that.
Let's start small, what if you want to have a single menu open or close. One approach could be to have something like
The, maybe obvious, next step is what if I have two menus?
but the above code is neither pretty nor scalable (what if I have 10 items in the menu?). So instead of representing the state of each possible menu you might want to keep track of which one is open/active
Now, we want the active route to change based on what is being clicked
Now you might ask, well that's all nice and dandy but how do I deal with a nested menu? like in your case.
You might notice in your existing code that all the
onClickhandlers happen in the deepest (the most nested)li/button, not on the first level of the menu.So lets say that on your first level you just want to add a class so it looks like that's the active menu.
I hope you can see a pattern in the above and apply that to your specific HTML requirements of using
ol,li,button. Hope this helps--
For anyone other than the OP reading this and thinking "this could be cleaner" or "we could use
useContext" or "we could add an abstraction here and here", save it. The verbosity on my answer to hopefully help the OP see the pattern so they can figure out a solution; this is not about showing off you know redux or mobX or how to use hooks.