I'm trying to change my circle Opacity when I click on it to act as an active state
I declared my state
const [active, setActive] = useState(false)
My function
const sidebarTrigger = () => {
setActive(true)
}
this is how i'm trying to change it, inside my MapContainer
fillOpacity= {active ? '0.9' : '0.7'}
MapContainer
<MapContainer whenCreated={setMap} center={[selectedRegion['lat'],selectedRegion['long']]} zoom={selectedRegion['zoom']} scrollWheelZoom={true}>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
{props.regionC.map((item,i) => {
<Circle
eventHandlers={{ click: sidebarTrigger.bind(this, item.city,item.lat,item.long) }}
center={item.city === 'Johannesburg' || item.city === 'Johhensburg' ? [-26.195246,28.034088] : [item.lat ,item.long]}
color= '#EE7E18'
fillColor="#EE7E18"
fillOpacity= {active ? '0.9' : '0.7'}
weight="0"
radius={radius}
/>
)
}
})}
</MapContainer>
The React Leaflet docs state that the props of child components are immutable:
so this explains why the opacity of the
<Circle>does not update when you change the value ofactive.If you use a ref, this will allow you to perform operations on the Circle. In the code below, we get a ref to the
<Circle>and theclickevent handler callsupdateActiveState()which updates the value of the active state:You can then define an effect that calls Leaflet's setStyle method to update the
fillOpacityof the Circle based on the value ofactive. This will be executed whenever the value ofactivechanges:See this StackBlitz for a working demo.