react-native-maps: Cannot call ref for methods on < MapView /> when using Function Components + Hooks
Question
I found this StackOverflow post with similar information on how this could be figured out and resolved, however I tried with both the useRef and createRef hooks and neither was able to capture a reference to the map in the way I can actually call a method on it.
https://stackoverflow.com/questions/41048546/how-can-i-attach-to-a-stateless-components-ref-in-react
One example:
import React, { createRef }, from "react"
<ommitted>
const ServiceMap = ({service, driver}) => {
let mapRef = createRef();
return (
<MapView
ref={mapref}
>
</MapView>
)
}
No matter what when I use the patterns and proposed solutions I get the following value from my “mapRef”:
Object {
"current": null,
}
Then when I try to call one of the methods on it, I am told that it is undefined. When I look up all of the documentation about how to use Refs, then I am never shown with this format, however this is the consistent way I am building my entire application with hooks. I would want to stay consistent with this approach.
When I read this article: https://www.robinwieruch.de/react-function-component I find that “React Function Components cannot have refs. However, if you need to pass a ref to a Function Component – because you have to measure the size of a function component’s DOM node, for example, or like in this case to focus an input field from the outside – you can forward the ref:”
So then let me try to forward the ref, ok. Hold on, I cannot because I did not create the component and I cannot modify the “<MapView>” component itself, I am importing it from this library!
Is there a solution here? Does this mean it is impossible to call methods on the components if I am using this approach? To clarify I can display the map, markers, etc. all just fine.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 5
- Comments: 16
@dragma How are you trying to access the methods? Can you show us what you’re trying to do and what error you’re getting?
FYI, make sure you’re accessing the methods through the .current property, i.e as
mapRef.current.methodNameSame thing here. I can’t have acces to any method in my
MapView’s ref. Code is almost the same, the “major” difference here is that I useuseRef.Code example :
I am using expo v35, and react-native-maps v0.25.0
Please try ref.current
For me this is working partially.
I have a component
<MapView ref={map} />and aconst map = useRef(null);declaration in aFunctionComponent. From within auseEffecteffect callback I can callmap.current.fitToCoordinates()for instance, which is working great. But from within anonChangeTexthandler of a<TextInput/>in the same component, themap.currentreference is null.Does anyone have any idea how to fix this?
Hello All, having same issue. Cant access methods animateToRegion when add reference with useRef(). Does anybody has working example with hooks?
I am trying to access the method by the
mapRefvariable. The output I get with this codeis this one
Any clue ?
Hey @mvuk.
So in MapView you attach the ref as mapref, but you’ve defined it as mapRef further above. Fix the typo where you attach the ref in MapView and you’re good to go. It should work.
Now, it’s true that functional components cannot have refs, in the sense that you can’t attach a ref to them. This is not what you’re doing here. You’re attaching a ref to MapView which is not a functional component, it’s a class component. So you shouldn’t worry about that.
As for createRef() vs useRef(), you should generally prefer useRef() in functional components since you probably want to maintain the reference across updates.