In my application I have two components, let's say Component1 and Component2, and a third component in charge of rendering the two.
Inside the third component, at the root level, I have an array that contains options among the 2 components, which look like this:
const RenderingComponent = () => {
const ref1 = useRef();
const ref2 = useRef();
const array = [{
title: '#1',
component: <Component1 ref={ref1} />,
...
}, {
title: '#2',
component: <Component2 ref={ref2} />
...
}];
...
}
Now, what happens is that Component1 gets initialized whilst Component2 doesn't. I tried to switch the order in the array and the exact other way round happens: Component2 gets initialized and Component1 doesn't.
I can confirm this since:
- I put console logs at the root level in the two Components and only the logs of the first component in the array work.
- The ref assigned to the second Component in the array always gives undefined on ref.current
PS: Both the components use forwardRef + useImperativeHandle to expose some methods.
Does anyone know what is happening here?