I have a react provider containing a number of components that I'm trying to get the order of in the DOM. I'm not concerned with the exact value that orders them but I need to know if one element is before or after another for other logic I need to perform in the provider. My app would look like this:
// App.jsx
return (
<Provider>
<Element /> // 1
{showSecond && <Element />} // 2
<div>
<Element /> // 7
</div>
<Element> // 14
<Element /> // 21
</Element>
</Provider>
);
// Element.jsx
const orderValue = getMagicOrderValue() // <-- Looking for this
const provider = useContext(providerContext);
useEffect(() => {
provider.add(orderValue);
return () => { provider.remove(orderValue); };
}, [orderValue, provider]);
return children;
Some of these components maybe nested or come and go per unrelated logic. I plan to report the ordering value to the provider context inside a useEffect and I'm not concerned should it change so long as the components then order correctly. Is there a build-in function or value that would help order these elements as they would appear in the dom?