Proper syntax for loading/exporting react components? - reactjs

I'm new to reactjs and am unable to debug this error: Element type is invalid: expected a string or a class/function but got: undefined. You likely forgot to export your component from the file it's defined in. Check the render method of Map.
Here is the code:
Map.js
import React, { Component } from 'react'
import { GoogleMapLoader, GoogleMap, Marker } from 'react-google-maps'
class Map extends Component {
render(){
const mapContainer = <div style={{height: '100%', width: '100%'}}></div>
return (
<GoogleMapLoader
containerElement = { mapContainer }
googleMapElement = {
<GoogleMap
defaultZoom={15}
defaultCenter={this.props.center}
options={{streetViewControl: false, mapTypeControl: false}}>
</GoogleMap>
} />
)
}
}
export default Map
Places.js
import React, { Component } from 'react'
class Places extends Component {
render(){
return (
<div>
Helloljlk
</div>
)
}
}
export default Places
App.js:
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import Map from './components/Map'
import Places from './components/Places'
class App extends Component {
render(){
const location = {
lat: 40.7,
lng: -73.98
}
return (
<div>
Hello
<div style={{width: 300, height: 600, background: 'red'}}>
<Map center={location} />
</div>
<Places />
</div>
)
}
}
ReactDOM.render(<App />, document.getElementById('app'))
I've looked up similar issues but haven't been able to find anything that works i.e. removing {} from import statements.

The component GoogleMapLoader has been deprecated in the version 6 onwards (please refer to the changelog here). So if you are using that version, you will get undefined when you import GoogleMapLoader. Instead, you should now be using withGoogleMap.
Map.js
import React, { Component } from 'react'
import { GoogleMapLoader, GoogleMap, Marker } from 'react-google-maps'
const Map = withGoogleMap(props => (
<GoogleMap
defaultZoom={15}
defaultCenter={props.center}
options={{streetViewControl: false, mapTypeControl: false}} />
));
App.js
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import Map from './components/Map'
import Places from './components/Places'
class App extends Component {
render(){
const location = {
lat: 40.7,
lng: -73.98
}
const mapContainer = <div style={{height: '100%', width: '100%'}}></div>;
return (
<div>
Hello
<div style={{width: 300, height: 600, background: 'red'}}>
<Map
center={location}
containerElement={mapContainer}
/>
</div>
<Places />
</div>
)
}
}

Related

I am trying to insert google maps in my project but getting an error

I am new to ReactJS, sorry if this sounds off
this is the error showen in console when compiling :
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in.
this is the piece of code to insert the componet in App.js :
import React, { Component } from 'react';
import './App.css';
import SimpleMap from './SimpleMap'
function App() {
return (
<div className="App">
<SimpleMap />
</div>
);
}
export default App;
SimpleMap code :
import React, { Component } from 'react';
import GoogleMapReact from 'google-map-react';
const AnyReactComponent = ({ text }) => <div>{text}</div>;
class SimpleMap extends Component {
static defaultProps = {
center: {
lat: 59.95,
lng: 30.33
},
zoom: 11
};
render() {
return (
// Important! Always set the container height explicitly
<div style={{ height: '100vh', width: '100%' }}>
<GoogleMapReact
bootstrapURLKeys={{ key: "AIzaSyAEKXIE54WnL96aA4qln5bEzU3uOGKqhyo"}}
defaultCenter={this.props.center}
defaultZoom={this.props.zoom}
>
<AnyReactComponent
lat={59.955413}
lng={30.337844}
text="My Marker"
/>
</GoogleMapReact>
</div>
);
}
}
export default SimpleMap;
It is because your SimpleMap component might be empty, Just paste this code in your component(SimpleMap)
import React, { Component } from 'react';
import { withGoogleMap, GoogleMap } from 'react-google-maps';
class Map extends Component {
render() {
const GoogleMapExample = withGoogleMap(props => (
<GoogleMap
defaultCenter = { { lat: 40.756795, lng: -73.954298 } }
defaultZoom = { 13 }
>
</GoogleMap>
));
return(
<div>
<GoogleMapExample
containerElement={ <div style={{ height: `500px`, width: '500px' }} /> }
mapElement={ <div style={{ height: `100%` }} /> }
/>
</div>
);
}
};
export default Map;
and add this google map script (https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY_GOES_HERE) in your index.html
Lastly just install react-google-maps package using yarn or npm
yarn add react-google-maps
npm install react-google-maps

how to display google map marker in google-map-react

hi all im trying to make google maps in react take lat and long from different component and display marker based on that. However the marker is not showing up when i put lat and long values. this.state.lat and long is definetly correct because my map does update but marker is nowhere to be found. Any help is greatly appreciated!
map.js
import React, { Component } from 'react';
import GoogleMapReact from 'google-map-react';
import marker from './marker.png';
import { geolocated } from 'react-geolocated';
const AnyReactComponent = ({ text }) => (
<div>
<img src={marker} style={{ height: '50px', width: '50px' }} />
</div>
);
export default class Map extends Component {
static defaultProps = {
zoom: 11,
};
Componentwillmount() {
console.log(this.props.center.latitude);
}
render() {
return (
<div className="google-map" style={{ width: '100%', height: '2000px', backgroundColor: 'red' }}>
<GoogleMapReact
options={{
styles:ExampleMapStyles
}}
center={this.props.center} defaultZoom={this.props.zoom}>
<AnyReactComponent
lat={this.props.center.latitude}
lng={this.props.center.longitude}
text={'Kreyser Avrora'}
/>
</GoogleMapReact>
</div>
);
}
}
home.js
import React, { Component } from 'react';
import axios from 'axios';
import './App.css';
import PlacesAutocomplete from 'react-places-autocomplete';
import { geocodeByAddress, geocodeByPlaceId, getLatLng } from 'react-places-autocomplete';
import UserForm from './UserForm.js';
import { classnames } from './helpers';
import marker from './marker.png';
import Nav from './nav';
import Food from './food';
import Tool from './tools';
import Health from './health';
import MapContainer from './Map';
import Map from './Map';
import { BrowserRouter as Router, Route, Link } from 'react-router-dom';
class Home extends Component {
constructor() {
super();
this.state = {
zoom: 13,
maptype: 'roadmap',
place_formatted: '',
place_id: '',
place_location: '',
address: '',
value:'',
latitude: 37.774929,
longitude: -122.419416,
marker: [],
place: [],
isLoaded2: false,
places2: [],
isLoaded: false,
isLoaded3: false,
};
}
render() {
return (
<div id="h">
<Map center={{ lat: this.state.latitude, lng: this.state.longitude }} />
<div />
);
}
}
export default Home;
You try to get the following attributes of the props object:
<AnyReactComponent
lat={this.props.center.latitude}
lng={this.props.center.longitude}
text={'Kreyser Avrora'}
/>
But the object you hand in the props has the attributes lat and lng:
center={{ lat: this.state.latitude, lng: this.state.longitude }}
Changing your AnyReactComponent to use this.props.center.lat and .lng should make it work.

Bug display with react leaflet

I start with react and I want to use a module that is called "react-leaflet" to make a map map.
But the problem is that I have a display bug with my map in the end on my page.
Can you help me please ?
Component Map:
import './Map.css';
import React, { Component } from 'react';
import { Map, Marker, Popup, TileLayer } from 'react-leaflet';
export default class MapLeaflet extends Component {
constructor(props) {
super(props);
this.state = {
lat: 51.505,
lng: -0.09,
zoom: 13
}
}
render() {
const position = [this.state.lat, this.state.lng];
console.log("dqdsqdq");
return(
<div id="map">
<Map center={position} zoom={13}>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution="© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors"
/>
<Marker position={position}>
<Popup>A pretty CSS3 popup.<br />Easily customizable.</Popup>
</Marker>
</Map>
</div>
);
}
}
Page to display Map:
import './Apies.css';
import React, { Component } from 'react';
import MapLeaflet from '../../components/Map/Map.js';
export default class Apies extends Component {
render() {
return (
<section className="Apies">
<main className="main">
<h2>Map</h2>
<MapLeaflet/>
</main>
</section>
);
}
}
Bug display: https://imgur.com/ojNDL3Z
It is required to import leaflet.css in your index.js and give a height to map container.
index.js:
import "leaflet/dist/leaflet.css";
MapLeaflet:
...
<Map style={{ height: "100vh" }} center={position} zoom={13}>
Demo

Google Maps With ReactJs

I'm using Google Map with React. But when I run my project, I get an error in Polygon.js file
TypeError: Cannot read property 'Component' of undefined
TypeError: Cannot read property 'array' of undefined
Please, let me know why?
This is snip code and image description for error:
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types'
import { GoogleAPIWrapper, InfoWindow, Marker } from 'google-maps-react'
export class MapContainer extends React.Component {
componentDidMount() {
this.loadMap();
}
loadMap() {
let map = new window.google.maps.Map(document.getElementById('map'), {
center: { lat: -33.8688, lng: 151.2195 },
zoom: 13,
mapTypeID: 'roadmap'
})
}
render() {
const style = {
width: "100%",
hight: "100%"
};
return (
<div id='map'>
</div>
);
}
}
export default GoogleAPIWrapper(
{
apiKey: "MY_KEY",
}
)(MapContainer);
Error_1
Error_2
It seems you have some errors in your code,
following the docs, you shouldn't be able to even export your component (typo in 'GoogleAPIWarapper').
working example
import {Map, InfoWindow, Marker, GoogleApiWrapper} from 'google-maps-react';
import React, { Component} from 'react';
export class MapContainer extends Component {
render() {
return (
<Map google={this.props.google} zoom={14}>
<Marker onClick={this.onMarkerClick}
name={'Current location'} />
<InfoWindow onClose={this.onInfoWindowClose}>
<div>
<h1>Test</h1>
</div>
</InfoWindow>
</Map>
);
}
}
export default GoogleApiWrapper({
apiKey: 'apiKey'
})(MapContainer)

HelpHelp to debug my React Code

First of all, I want to ask whether is my statement is correct :
"When we are making components, it is better to put it in different files"
Is it correct? since currently I tried so, and it cause problems.
Parents :
import React, { Component } from 'react';
import './App.css';
import {SubmitComponent} from './submitComponent.jsx';
import {topicTable} from './topicTable.jsx';
// import {TopicsContainer} from './TopicsContainer.js'
const dashboardStyle = {
border: '2px solid black',
width: '70%',
height: 'auto',
marginLeft: 'auto',
marginRight: 'auto',
marginBottom: '100px',
};
class AppDashBoard extends Component {
constructor(props) {
super(props);
this.state = {
datas: []
}
}
submitTopic = (data) => {
console.log(data);
console.log(this.state.datas);
console.log(this.state.datas.concat([data]));
this.setState({
datas: this.state.datas.concat(data)
});
console.log(this.state.datas);
}
render() {
return (
<div style={dashboardStyle}>
<h1 style={{textAlign:'center'}}>Coding Exercise</h1>
<hr />
<SubmitComponent submitTopic={this.submitTopic} />
<topicTable topics={this.state.datas} />
</div>
);
}
}
export default AppDashBoard
and this is the topicTable component :
import React, {Component} from 'react';
import {oneTopic} from './oneTopic.jsx';
export class topicTable extends Component {
render() {
const topicstable = this.props.topics.map((topic) => (
<oneTopic
title={topic.title}
desc = {topic.desc}
vote = {topic.vote}
/>
));
console.log("HAHAHAHA");
return (
<div id="topics">
{oneTopic}
</div>
);
}
}
and Lastly, my oneTopic component:
import React, {Component} from 'react';
export class oneTopic extends Component {
render() {
return(
<div style={{width:'96%', height:300, backgroundColor :'#AAA'}}>
<h1>HAHAHAHA</h1>
</div>
);
}
}
My problems are :
1) In the topicTable component, the console.log("HAHAHA") is not executed at all, I wonder why ?
2) Also for the oneTopic, the HAHAHA is not showing at all.
Even I already export and import everything
Please help
ReactJS component names must begin with capital letters.
1) rename oneTopic to OneTopic
2) change {oneTopic} to <oneTopic />

Resources