I have two identical JSON objects, such that:
JSON.stringify(obj1) == JSON.stringify(obj2); // returns true
Also if I log them in the console, I can inspect both of them properly and everything works as expected. However for some reason I cannot access the values of obj2:
console.log(obj1.type) // returns "FeatureCollection"
console.log(obj2.type) // returns undefined
The json data is loaded from file and looks like this (data.json):
{
"type": "FeatureCollection",
"name": "data",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"}
},
"features": [
{
"type": "Feature",
"properties": {"id": 0, "qualities": ["q1", "q2", "q3"]},
"geometry": {"type": "Point", "coordinates": [25.227297, 36.418335]}
}]
}
Here is the difference: This scenario is happening in a react app (in .jsx file). obj1 is loaded directly from file in the .jsx file. obj2 is passed into the react component and accessed through this.props. It was previously loaded with same syntax from same file. These are the two files:
leaflet-map.jsx:
import React from "react";
import * as geojson from "../assets/data.json";
export default class Map extends React.Component {
render(){
var obj1 = geojson;
var obj2 = this.props.geojson;
console.log(JSON.stringify(obj1) == JSON.stringify(obj2));
console.log(obj1.type);
console.log(obj2.type);
return (<div></div>);
}
}
LeafletMap.jsx
import React from "react";
import Map from "../../../components/leaflet-map";
import * as geojson from "../../../assets/data.json";
export default function LeafletMap() {
return (<Map geojson={geojson}></Map>);
}
What is happening? This is so strange I don't even know where to start looking...
Update:
I tried @TheMaster 's hint and changed Map class definition to:
export default class Map extends React.Component {
constructor(props){
super(props);
console.log(this.props.geojson);
console.log(this.props.geojson.type);
}
render(){
return (<div></div>);
}
}
Result:
So appearently the constructor is called twice(?).

