Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The contents of <Control> do not get rendered on initial <Map> display. #27

Closed
cherouvim opened this issue Oct 3, 2018 · 10 comments · Fixed by #32
Closed

The contents of <Control> do not get rendered on initial <Map> display. #27

cherouvim opened this issue Oct 3, 2018 · 10 comments · Fixed by #32

Comments

@cherouvim
Copy link
Contributor

cherouvim commented Oct 3, 2018

When using this code:

<Control position="bottomleft" className="xxx">
  <button className="yyy">Foo</button>
</Control>

The initial <Map> render results in the following HTML which doesn't really contain the contents of the custom control, just its wrapper:

<div class="xxx leaflet-control"></div>

As soon as the next <Map> render takes place, the custom control HTMLs is finalized to:

<div class="xxx leaflet-control"><button class="yyy">Foo</button></div>

Live example: https://codesandbox.io/s/n4y9opn000

@AnDrOlEiN
Copy link

AnDrOlEiN commented Oct 3, 2018

Faced with this issue too. Do you have any suggestions how to fix it without using forceUpdate?

@cherouvim
Copy link
Contributor Author

I have no idea.

@cherouvim
Copy link
Contributor Author

This happens because:

This is just an external observation of the problem though. Not sure why this happens or what to do next.

@cherouvim
Copy link
Contributor Author

cherouvim commented Oct 17, 2018

A temp solution until this is fixed is to do a this.forceUpdate(); when your custom control component mounts. e.g:

import React, { Component } from "react";
import Control from "react-leaflet-control";

export default class FooControl extends Component {

    componentDidMount() {
        this.forceUpdate();
    }

    render() {
        return (
            <Control position="topleft" className="FooControl">
                <select>
                    <option>foo</option>
                    <option>bar</option>
                </select>
            </Control>
        );
    }
}

@ZhangYiJiang
Copy link
Contributor

ZhangYiJiang commented Oct 17, 2018

A dumb fix would be for <LeafletControl> to forceUpdate itself when it mounts (equivalent to the fix above, but at least it hides the hack)

@bslipek
Copy link

bslipek commented Nov 5, 2018

How about that:

https://codesandbox.io/s/pj5k2v8l9x?module=%2Fsrc%2FControl.js

@barbalex
Copy link

I have run into the same problem. As I am using function components and react hooks there is no forceUpdate.

So in order to force an update on component mount I set state like this:

const [controlType, setControlType] = useState('coordinates')
useEffect(() => setControlType('coordinates'), [])

where setControlType('coordinates') is just (unnecessarily if it weren't for this issue) setting the default value for controlType, which is just a piece of state I happen to use in this component.

rajadain added a commit to project-icp/bee-pollinator-app that referenced this issue Dec 28, 2018
rajadain added a commit to project-icp/bee-pollinator-app that referenced this issue Jan 2, 2019
rajadain added a commit to project-icp/bee-pollinator-app that referenced this issue Jan 2, 2019
@cherouvim
Copy link
Contributor Author

I've updated my reproduce link https://codesandbox.io/s/n4y9opn000 with the latest dependencies including "react-leaflet-control": "2.1.0", but the problem still exists.

@ZhangYiJiang
Copy link
Contributor

I think there might have been a packaging issue. https://unpkg.com/react-leaflet-control@2.1.0/dist/control.js does not contain componentDidMount and is identical to v2.0.0

@Roschl
Copy link

Roschl commented Mar 11, 2020

This problem occurs again on v2.1.2, which contains componentDidMount

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants