【问题标题】:react selected object, can't show what i want反应选定的对象,无法显示我想要的
【发布时间】:2019-11-05 03:52:12
【问题描述】:

我想在我的 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 类包含所有数组。

【问题讨论】:

    标签: javascript arrays reactjs autodesk-forge


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 2022-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多