我想在我的react组件中使用json形式的字符串。
用于调用我的图片并将其放在我的Web应用程序中的路径(字符串)。
问题是当我调用此img时,它将调用我的所有数组。不仅是所选的。
我该怎么办?
这是我的代码:
import React, { Component } from 'react';
import './poiList.css';
// Composant react affichant la liste des points d'intérêts
// C'est ce composant qui communique avec l'extension POI
class PoiList extends Component {
constructor(props) {
super(props);
this.state = {
poi: [],
selectedPoi: undefined,
settings: {
defaultPointColor: [61, 183, 255],
selectedPointColor: [37, 27, 255],
defaultPointTransparancy: 0.4,
spriteScaleFactor: 0.8,
//altitude par défaut
spriteAltitude: 15.0
}
};
}
componentDidMount() {
fetch('/settings')
.then(response => {
if (!response.ok) {
console.log(`status ${response.status}`);
throw new Error(`status ${response.status}`);
}
return response.json();
})
.then(json => {
this.setState({
settings: json
});
})
.catch(e => {
console.log(`Settings call failed: ${e}`);
this.setState({
message: `Settings call failed: ${e}`
});
})
fetch('/poi')
.then(response => {
if (!response.ok) {
console.log(`status ${response.status}`);
throw new Error(`status ${response.status}`);
}
return response.json();
})
.then(json => {
this.setState({
poi: json.poi
});
})
.catch(e => {
console.log(`POI call failed: ${e}`);
this.setState({
message: `POI call failed: ${e}`
});
});
}
render() {
if(this.props.poiExtension !== undefined) {
this.props.poiExtension.clearAllPOI();
this.props.poiExtension.setSettings(this.state.settings);
}
let self = this;
let poiList = [];
let poiPath = [];
let index = 0;
this.state.poi.map((poi) => {
if(this.props.poiExtension !== undefined) {
this.props.poiExtension.createPOI(poi, this.state.selectedPoi === poi);
}
let colorState = index % 2 === 1 ? "white" : "";
colorState = this.state.selectedPoi === poi ? "selected" : colorState;
let classC = "poiEntry " + colorState;
let poiItem = (
<div className={classC} key={index} onClick={() => {
self.setState({
selectedPoi: poi,
});
****** *******console.log(poi.path);*************
}}>
{poi.type} ({poi.x}, {poi.y}, {poi.z})
</div>
);
index++;
let poiItem2 = (
<div className={classC} key={index} onClick={() => {
self.setState({
selectedPoi: poi,
});
console.log(poi.path);
}}>
{poi.path}
</div>
);
poiList.push(poiItem);
poiPath.push(poiItem2);
})
return(
<div className="fullscreen">
<div className="poiTitle">BATIMENT E17</div>
<div className="poiList">
{ poiList }
</div>
<img className="poiDesc"
src={ poiPath } >
</img>
</div>
);
}
}
export default PoiList;
现在是json示例
{
"points": [
{
"x" : 23000.0,
"y" : 7000.0,
"z" : 1200.0,
"type": "Défaut",
"path":"./frontend/public/image/1.PNG"
},
{
"x" : -2000.0,
"y" : -3000.0,
"z" : 1400.0,
"type": "Défaut",
"path":"./frontend/public/image/2.jpg"
},
{
"x" : 10000.0,
"y" : -3000.0,
"z" : 1400.0,
"type": "Défaut",
"path":"./frontend/public/image/3.jpg"
},
{
"x" : -2000.0,
"y" : 3000.0,
"z" : 1400.0,
"type": "Défaut",
"path":"./frontend/public/image/4.jpg"
},
{
"x" : 6000.0,
"y" : 6500.0,
"z" : 1400.0,
"type": "Défaut",
"path":"./frontend/public/image/5.jpg"
},
{
"x" : 13000.0,
"y" : 1000.0,
"z" : 1400.0,
"type": "Défaut",
"path":"./frontend/public/image/6.jpg"
},
{
"x" : 13000.0,
"y" : -15000.0,
"z" : 1400.0,
"type": "Défaut",
"path":"./frontend/public/image/7.jpg"
},
{
"x" :28000.0,
"y" : 0.0,
"z" : -1000.0,
"type": "Défaut",
"path":"./frontend/public/image/8.jpg"
}
]
}
正如我所说,我可以一次调用所有照片,但我不知道如何选择所选的照片。脚本中的console.log显示了我想要的内容,但是我不能将其放入变量中。 ..
poiDesc类包含所有数组。
答案 0 :(得分:1)
您的代码按预期工作,因为您将所有路径对象都推送到了img标签:
let poiPath = [];
...
poiPath.push(poiItem2);
...
<img className="poiDesc"
src={ poiPath } >
</img>
您应该使用状态来获取/设置图像的路径以与您的选择同步:
<div className={classC} key={index} onClick={() => {
self.setState({
selectedPoi: poi,
selectedPath: poi.path
});
...
<img className="poiDesc"
src={ this.state.selectedPath } >
</img>