LiveBy / react-leaflet-control

Dumb React component for creating Controls
ISC License
88 stars 24 forks source link

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

Closed cherouvim closed 5 years ago

cherouvim commented 6 years ago

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 commented 6 years ago

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

cherouvim commented 6 years ago

I have no idea.

cherouvim commented 6 years ago

This happens because:

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

cherouvim commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

How about that:

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

barbalex commented 5 years ago

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.

cherouvim commented 5 years ago

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 commented 5 years ago

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 commented 4 years ago

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