【发布时间】:2020-05-25 22:55:54
【问题描述】:
我目前正在做一个学校项目。我有一个地图位置应用程序。但不知何故,我需要传递状态值坐标。但是我不能把它分享到下一堂课……有人可以帮我吗?
代码:
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 };
我尝试使用 props.state.coordinates[0],我还使用了全局变量,并在函数 GetLocation() 中设置了值,然后将其用于我的笔记本电脑版本。但是有人可以帮助我吗?
提前致谢。
【问题讨论】:
-
您能否通过添加以下信息来改进您的答案:1. 组件的结构 2. 包含状态的组件 3. 需要使用状态的组件。
-
@SorcererApprentice 这些是我的卡片。在我的 App.js 中,我包含了这些笔记本电脑和移动版本。手机有手机png,笔记本有笔记本png。与彼此的地图位置。但是在移动卡中,您可以访问位置。然后你得到经度和纬度
-
您要将数据传递到哪个类?您可以简单地执行
然后在 MyComponent中您可以使用this.props.data访问它。还是我错过了什么? -
@RahulDwivedi 我在移动组件中有数据,而不是在应用程序(全局组件)中,因为我想要移动组件中的按钮访问该位置,然后在笔记本电脑组件中添加值
-
那么,如果我理解正确,您只想将您的位置信息传递到您的
laptop组件中?
标签: javascript reactjs jsx