𝗥𝗲𝗮𝗰𝘁 + 𝗠𝗮𝗽𝗯𝗼𝘅 𝗚𝗟 𝗝𝗦: 𝗖𝘂𝘀𝘁𝗼𝗺 𝗠𝗮𝗿𝗸𝗲𝗿𝘀 𝗔𝗻𝗱 𝗣𝗼𝗽𝘂𝗽𝘀

Mapbox and React often fight. Mapbox manages its own DOM. React uses a virtual DOM. This makes custom markers hard to build.

Use React portals to fix this. Portals render React content into Mapbox elements.

Use this pattern:

React handles the component life. It adds markers when data arrives. It removes markers when data leaves. Mapbox handles the position.

This works for:

You get clean code. You get custom styles. You get a smooth user experience.

Source: https://dev.to/chriswhong/react-mapbox-gl-js-custom-markers-popups-and-bounds-based-data-fetching-4adj