gpt4 book ai didi

javascript - react 选定的对象,无法显示我想要的

转载 作者:行者123 更新时间:2023-12-02 23:26:21 31 4
gpt4 key购买 nike

我想在我的 React 组件中采用 json 形式的字符串。

调用我的图像并将其放在我的网络应用程序上的路径(字符串)。

问题是,当我调用这个 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 显示了我想要获取的内容,但我无法将其放入变量中。 ..

enter image description here

poiDesc 类包含所有数组。

最佳答案

您的代码正在按预期工作,因为您将所有路径对象推送到 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>

关于javascript - react 选定的对象,无法显示我想要的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56717946/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com