I am currently working on a school project. I have a map location app. But somehow I need to pass state value coordinates. But I can't share it into the next class... Can someone help me out?
Code:
import React from 'react';
import '../style/card.css';
import '../js/functions/maps';
import Button from './button.js';
import { getLocation } from './functions/getLocation.js';
import './functions/maps';
class Mobile extends React.Component {
state = {
coordinates: {}
};
getLocationCoordinates = () => {
getLocation((coordinates) => {
this.setState({ coordinates });
console.log(this.state.coordinates);
});
};
functionCoordinatePrint = (index) => {
return this.state.coordinates[index] ? this.state.coordinates[index] : Math.random() * 50 + 1;
};
render() {
const { lat, lng } = this.state.coordinates;
return (
<div id={'location'} className={'card'}>
<div className={'card-layout'}>
<div className={'text-card'}>
<h1>{this.props.header}</h1>
{this.props.text !== '' && <p className={'max-width-70'}>{this.props.text}</p>}
<div
style={{
color: 'var(--color-text)'
}}
>
<input type="checkbox" id="conditions-read" />
<a href="../pages/algemene-voorwaarden">Accepteer voorwaarden</a>
</div>
<Button function={this.getLocationCoordinates} text={'Allow Location'} />
<p>{this.props.coordinaten}</p>
</div>
<div className={'mobile'}>
<img src={this.props.device} className={'device'} alt={this.props.alt_image} />
<div className={'overlay'}>
<div id={'location-maps'}>
<iframe
title="map"
frameBorder="0"
scrolling="no"
marginHeight="0"
marginWidth="0"
src={`https://www.mapquest.com/near-${this.functionCoordinatePrint(
'latitude'
)},${this.functionCoordinatePrint('longitude')}?zoom=3`}
/>
</div>
</div>
</div>
</div>
</div>
);
}
coordinatesToArray = coordinatesToArray[
(this.functionCoordinatePrint('latitude'), this.functionCoordinatePrint('longitude'))
];
}
class Laptop extends React.Component {
render() {
return (
<div id={'recommendations'} className={'card'}>
<div className={'card-layout'}>
<h1>{this.props.header}</h1>
<div className={'laptop'}>
<img
style={{
width: '150%'
}}
src={this.props.device}
alt={this.props.alt}
/>
<div className={'overlay-laptop'}>
<div className={'maps'}>
<iframe
title="mapExample"
frameBorder="0"
scrolling="no"
marginHeight="0"
marginWidth="0"
src={
'https://www.mapquest.com/near-' +
(this.props.lat ? this.props.lat : Math.random() * 30 + 1) +
',' +
(this.props.long ? this.props.long : Math.random() * 30 + 1) +
'?zoom=3'
}
/>
</div>
</div>
</div>
</div>
</div>
);
}
}
export { Mobile, Laptop };
I've tried to use props.state.coordinates[0], I also used a global var and have set the values in function GetLocation() and then use it into my Laptop version. But can someone help me?
Thanks in advance.