I am stuck on some Basic problem and don't know what should I look for... I have tried to work with Props but I failed.
I have two Files.
First File Map.js
export default function Map() {
return (
<View style={styles.container}>
<MapView style={styles.map}
initialRegion={{
latitude: 37.78825,
longitude: -122.4324,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}}
/>
</View>
);
}
Second File CurrentPossition.js
export default function CurrentPossition() {
const [location, setLocation] = useState(null);
const [errorMsg, setErrorMsg] = useState(null);
const [copiedText] = useState('')
useEffect(() => {
(async () => {
if (Platform.OS === 'android' && !Constants.isDevice) {
setErrorMsg(
'Oops, this will not work on Snack in an Android emulator. Try it on your device!'
);
return;
}
let { status } = await Location.requestPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission to access location was denied');
return;
}
let location = await Location.getCurrentPositionAsync({});
setLocation(location);
})();
}, []);
let text = 'Waiting..';
if (errorMsg) {
text = errorMsg;
} else if (location) {
text = JSON.stringify(location.coords.latitude);
}
I would like to pass the Value from the CurrentPossition.js Variable let text
in to the latitude:parameter of <MapView /> component of the Map.js File.
<MapView style={styles.map}
initialRegion={{
latitude: {text},
}}
/>
How can I do this?
2nd Try
My Map.JS looks now like this, but I don't become the right coordinates.
When I log the Value of text - I just become Waiting... that's the string from
const [text, setText] = React.useState("Waiting...");
export const MyContext = React.createContext();
export default function Map() {
const [text, setText] = React.useState("Waiting...");
console.log(text);
return (
<MyContext.Provider value={{ text, setText }}>
<View style={styles.container}>
<MapView
style={styles.map}
initialRegion={{
latitude: text,
longitude: -122.4324,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}}
/>
</View>
</MyContext.Provider>
);
}```