My app structure is as follows (HighOrderComponent
is Alt's AltContainer
wrapper):
<App>
<HighOrderComponent store={someStore...}>
<MyCanvasComponent ref="canvasRef" />
</HighOrderComponent>
</App>
I need to expose a collectData()
method from the MyCanvasComponent
since the canvas needs to transform its content to a base64 image once a button is clicked in the App
component.
I would like, in the button
's click
handler, to be able to use this.refs.canvasRef.collectData()
.
Unfortunately, it seems that nested components refs
are not being taken into account. I know that generally refs
should be considered private (i.e. belong to the HighOrderComponent
) but -
They are not available on it as well, meaning if I assign a
ref="highOrderComponentRef"
thenthis.refs.highOrderComponentRef
would work butthis.refs.highOrderComponentRef.refs.canvasRef
wouldn't.The
HighOrderComponent
is a generic component and therefore does not know what its children would be.
The this.props.children
on the HighOrderComponent
are of ReactElement
type which is lightweight and does not expose their methods.
The only solution so far is to pass the MyCanvasComponent
a callback in which it will send itself and a reference will be saved, but this is tedious and against best practices as per React 0.13 release notes
ref resolution order has changed slightly such that a ref to a component is available immediately after its componentDidMount method is called; this change should be observable only if your component calls a parent component's callback within your componentDidMount, which is an anti-pattern and should be avoided regardless