I have issue keeping ref of nested original element while cloned parent gets unmounted. Not sure what I am doing wrong.
Have page with structure like this
<Sortable>
<Collapsible ref='Collapsible-1'>...</Collapsible>
<Collapsible ref='Collapsible-2'>...</Collapsible>
</Sortable>
- Sortable component wraps each children to Sortable__item component
- When user starts sorting(dragging) one of Sortable__item components I React.cloneElement() original Sortable__item to display it as dragable shadow
- It gets cloned with all children, in this scenario with Collapsible component on which Page has ref['Collapsible-1'] saved. That ref on page gets changed to this shadows ref.
- Once touchEnd kicks in I update state of Sortable to not show shadow(it gets unmounted).
- When it gets unmounted it also removes ref inside Page(changes to null)
- ISSUE: page doesn't have ref to original Collapsible as first its got changed to shadow, and then shadow got unmounted so now its null
Quick hack/fix to solve this annoying issue. This way refs never get updated if they already exist. This is pretty bad, but I don't know how else prevent this. Anyone can point me to right direction?
let registerRef = function(name, item){
if(this.items[name]) return;
this.items[name] = item;
}
<Sortable>
<Collapsible ref={registerRef.bind(this,'Collapsible-1')}>...</Collapsible>
<Collapsible ref={registerRef.bind(this,'Collapsible-2')}>...</Collapsible>
</Sortable>