React-mapbox-gl: Can't clear layer filter after being set

Created on 25 Oct 2019  路  4Comments  路  Source: alex3165/react-mapbox-gl

You are currently not able to clear a layer filter after it has been set.

For example: you set a filter on a layer to show a subset of the features by setting the prop. At some point, you want to clear your filter to show all features in that layer again. Setting the filter prop back to null does nothing.

The issue lies in this part of the componentWillReceiveProps

if (props.filter && filter && !isEqual(props.filter, filter)) {
    map.setFilter(id, props.filter || [])
}

This would fix the issue

if (!isEqual(props.filter, filter)) {
    map.setFilter(id, props.filter || [])
}

Mapbox says that map.setFilter(null) or map.setFilter(undefined) is the intended way to remove any existing filters off of a layer. See: https://docs.mapbox.com/mapbox-gl-js/api/#map#setfilter

Most helpful comment

sorry for the delay & thanks for pointing this out 鈥撀燼 fix was released under v4.8.2

All 4 comments

Also passing setFilter [] is invalid. Mapbox throws console errors when you try to do that. The proper way to set to an empty filter is by passing null.

sorry for the delay & thanks for pointing this out 鈥撀燼 fix was released under v4.8.2

sorry for the delay & thanks for pointing this out 鈥撀燼 fix was released under v4.8.2

Much appreciated sir

Hello @mklopets,
seems geojson-layer has the same problem.
I've created a PR with a small fix.
Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

spybaby1 picture spybaby1  路  4Comments

jonheslop picture jonheslop  路  4Comments

loverdeveloper picture loverdeveloper  路  3Comments

madeleinejohanson picture madeleinejohanson  路  4Comments

Ricardo-C-Oliveira picture Ricardo-C-Oliveira  路  4Comments