0

I have two files index.js and map.js. In map.js I have Kartta component, that contains a leaflet map and a method fetchAll() that creates markers on the map from the coordinates that it gets from API, which in this case is test.json. It works fine if I put the method inside componentDidMount(), it creates the markers on load. However I want to create those markers by clicking the button "Haku", which is located inside Sidebar component in index.js.

Here is map.js

import React, { Component } from 'react';
import L from 'leaflet';
import { Map, Marker, Popup, TileLayer } from 'react-leaflet';
import { fetchAll } from './search';

const position = [62.241581, 25.758742];

class Kartta extends Component {
  state = {
      kohteet: []
  }

  componentDidMount() {
    this.fetchAll();
  }

  fetchAll = () => {
    console.log("täällä");
    fetch('test.json')
    .then(res => res.json())
    .then((data) => {
        this.setState({ kohteet: data })
    })
    .catch(console.log(this.state.kohteet))
  }

  iconSize = (iconName) => {
    if (iconName == 'tree.png') {
      return 30, 30
    }
    else {
      return 15, 30
    }
  }

  render() {
      return (
        <div id="mapid" className="h-100 w-100">
          <Map center={position} zoom={7}>
          <button onClick={this.haeKaikki}>Hae</button>
            <TileLayer
              url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
              attribution="&copy; <a href=&quot;http://osm.org/copyright&quot;>OpenStreetMap</a> contributors"
            />
            {this.state.kohteet.map((kohde) => (
                <Marker key={'key' + kohde.id} position={[kohde.lon, kohde.lat]} icon={
                    new L.Icon({
                        iconUrl: require('../img/' + kohde.icon),
                        iconSize: new L.Point(this.iconSize(kohde.icon)),
                        id: kohde.id + kohde.icon
                    })
                }>
                    <Popup>
                        <h1>{kohde.name}</h1>
                        <p>{kohde.desc}</p>
                    </Popup>
                </Marker>
              ))}

          </Map>
        </div>
      )
  }
}

export default Kartta

Here is index.js

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import './css/main.css';
import 'bootstrap/dist/js/bootstrap.bundle.js';
import 'leaflet/dist/leaflet.css';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faSearch, faInfoCircle } from '@fortawesome/free-solid-svg-icons';
import Kartta from './components/map';

// Yläpalkki
class Navbar extends React.Component {
    render() {
        return (
            <nav id="nav-bar" className="navbar navbar-expand-lg navbar-dark bg-primary">
                <a id="nav-bar-a0" className="navbar-brand" href="/">MeijänMetsät</a>
                <button id="toggle-search-btn" type="button" href="#sidebar" data-toggle="collapse" className="btn btn-primary">
                    <FontAwesomeIcon icon={faSearch} />
                    <span> Haku</span>
                </button>
                    <ul id="nav-bar-ul0" className="navbar-nav mr-auto">

                    </ul>
                    <div id="nav-bar-div0" className="dropdown">
                        <button id="nav-bar-button0" className="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Kirjaudu
                        <span id="nav-bar-span0" className="caret"></span></button>
                        <div id="dropdown-div0" className="p-2 dropdown-menu dropdown-menu-right" style={{"minWidth":"350px"}}>
                            <form id="dropdown-form0">
                                <div id="dropdown-div1" className="form-group">
                                    <label id="dropdown-label0" htmlFor="exampleInputEmail1">Sähköposti</label>
                                    <input id="dropdown-input0" type="email" className="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Syötä sähköposti" />
                                </div>
                                <div id="dropdown-div2" className="form-group">
                                    <label id="dropdown-label1" htmlFor="exampleInputPassword1">Salasana</label>
                                    <input id="dropdown-input1" type="password" className="form-control" id="exampleInputPassword1" placeholder="Salasana" />
                                </div>
                                <div id="dropdown-div3" className="row">
                                    <button id="dropdown-button0" type="submit" className="ml-3 btn btn-primary">Kirjaudu</button>
                                    <button id="dropdown-button2" type="submit" className="ml-2 btn btn-primary">Rekiströidy</button>
                                    <button id="dropdown-button1" type="submit" className="ml-2 btn btn-secondary">Facebook</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </nav>
        );
    }
}

// Sivupalkki
class Sidebar extends React.Component {
    render() {
        return (
            <div className="p-2 collapse in" id="sidebar">
                <div className="list-group panel" id="sidebar-div0">
                    <form id="sidebar-form0">
                        <div id="sidebar-div01" className="form-group active">

                            <label id="sidebar-label0" htmlFor="exampleInputEmail1">Kohteen nimi</label><span> </span>
                            <FontAwesomeIcon icon={faInfoCircle} className="pointer dropdown-toggle" data-toggle="dropdown" alt="Hakuohjeet" />
                            <div id="search-info" className="p-2 dropdown-menu dropdown-menu-right">
                                <p>Hae kirjoittamalla tägejä</p>
                            </div>
                            <div className="row">
                                <div className="col-7 ml-3 p-0">
                                    <input type="text" className="form-control" id="searchByName" aria-describedby="emailHelp" placeholder="Syötä kohteen nimi" />
                                </div>
                                <button id="sidebar-button0" type="submit" className="btn btn-primary ml-2 col-3" onClick={() => Kartta.fetchAll}>Haku</button>
                            </div>

                        </div>
                    </form>
                    <div id="sidebar-div02" className="dropdown">
                        <a id="sidebar-button1" className="btn btn-light dropdown-toggle p-0 thaku" type="button" data-toggle="dropdown">Tarkennettu haku
                        <span id="sidebar-span0" className="caret"></span></a>
                        <div id="sidebar-div020" className="p-0 dropdown-menu border-0 mt-2 w-100 h-100">
                            <form id="sidebar-form1">
                                <div id="sidebar-div0200" className="form-group p-0">
                                    <label id="sidebar-label1" htmlFor="exampleInputEmail1">Paikkakunta</label>
                                    <input id="sidebar-input1" type="text" className="form-control" placeholder="Syötä paikkakunta" />
                                </div>
                                <div id="ch-div0" className="row pl-3">
                                    <label id="lbl-location">Sijainti</label>
                                    <input id="ch-location" className="mt-2 ml-2" type="checkbox" aria-label="Checkbox for following text input" />
                                </div>
                                <div className="input-group mb-3">
                                <div className="input-group-prepend">
                                    <label className="input-group-text" htmlFor="inputGroupSelect01">Palvelut</label>
                                </div>
                                    <select className="custom-select" id="inputGroupSelect01">
                                        <option defaultValue>Valitse...</option>
                                        <option value="1">Kakkapaikka</option>
                                        <option value="2">Pissapaikka</option>
                                        <option value="3">Kaljapaikka</option>
                                    </select>
                                </div>
                                <div id="dropdown-div3" className="row p-0">
                                    <button id="dropdown-button0" type="submit" className="ml-3 btn btn-primary">Tarkennettu haku</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
        </div>
        );
    }
}

// Sisältö
class Content extends React.Component {
    render() {
        return (
            <div id="main-container" className="container-fluid h-100 p-0">
                <Navbar />
                <div id="container0" className="row h-100 w-100 m-0 wrapper">
                    <Sidebar />
                    <div id="container02" className="col h-100 w-100 p-0">
                    <Kartta />
                    </div>
                </div>
            </div>
        );
    }
}

ReactDOM.render(<Content />, document.getElementById('root'));

The button "Haku" has onClick={() => Kartta.fetchAll}, but it does not do anything, it isn't even accessing the fetchAll() function. The function works perfectly if I create the button on the map, but I don't want to do that, the button has to be on the Sidebar as the plan is to use a search word in the future.

Thank you!

1
  • For manipulating state between 2 or more comps you have to use either redux or context. If your app is small use the latter.
    – kboul
    Commented Mar 24, 2020 at 11:29

1 Answer 1

0

you need to create a prop e.g. createmap:booleanin Kartta component. Now in index.js firstly you need to create a state e.g. createmap:boolean (initially set to false) and upon Haku button click you need to set the state:

this.setState({
creatmap:true;
});

you need to include Katta component JSX in index.js render() function e.g.

<Kartta createmap = {this.state.createmap} />

Now in Kartta component on componentDidMount()you can check createmap' prop and based on its value call the functionfetchAll()`:

public componentDidMount() {
if(this.prop.createmap) {
fetchAll();
}
}

what this will do is causing rerendering due to setting state upon button click and will call 'fetchAll()' function if createMap is found true

Hope this helps.

EDIT:

Don't forget to use export keyword with your Kartta component class so you can import it in index.js to create its JSX in render():

export default class Kartta extends Component { ... }

EDIT 2

In your navbar component class initialize state in constructor:

export default class NavBar extends Component {

  constructor(props) {
    super(props);
    this.state: {
    createmap: false;
    }
   }
}

your Kartta component class:

 export default class Kartta extends Component {
   constructor(props) {
    super(props); 
}
   componentDidMount() {
    if(this.props.createmap) {
    fetchAll();
   }

 }
3
  • I am new to react so can you elaborate on where in index.js should I create a state? In Sidebar component? Thanks Commented Mar 24, 2020 at 13:01
  • I've done all of the above except public componentDidMount() as I couldn't use public in js file, seems like it can be used only in ts files, so I left public out. I also got a warning that interface can only be used in ts files. After all that I am getting an error: TypeError: Cannot read property 'createmap' of null. Commented Mar 24, 2020 at 14:05
  • check updated answer for js file. Answer was written according to TypeScript before Commented Mar 24, 2020 at 14:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.