question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[question] Example of new context API in use

See original GitHub issue

Can we get a quick example of the new context API for those upgrading from react-mapbox-gl 3.9.x to 4.0.0?

In my case I’m doing the following:

class MyComponent extends React.Component<Props> {
    static contextTypes = {
        map: PropTypes.object.isRequired
    };
}

To then be able to access the map object to update the cursor type when hovering elements, e.g.

circleOnMouseEnter={event => {
    map.getCanvas().style.cursor = "pointer";
    onEnterFeature(event.features[0]);
}}

Thanks!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
daniel-hausercommented, Nov 8, 2018

event.target is the map

circleOnMouseEnter={event => {
    const map = event.target;
    map.getCanvas().style.cursor = "pointer";
    onEnterFeature(event.features[0]);
}}
1reaction
redbmkcommented, Jan 28, 2020

@nathanredblur If you want to use context, but don’t want to render anything there, you can just return null in that function. You could also use hooks to access the map in a component: const map = React.useContext(MapContext)

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Context API: A deep dive with examples - LogRocket Blog
In this tutorial, we'll explore how we can use React Context to avoid prop drilling.
Read more >
20 React Context API Interview Questions and Answers - CLIMB
Here are 20 commonly asked React Context API interview questions and answers to ... First, the new API is more flexible and easier...
Read more >
How to Work with the React Context API - Toptal
Once that's done, we can import the context and use it to create our provider, which we're calling MyProvider. In it, we initialize...
Read more >
A Guide to React Context and useContext() Hook
The React context provides data to components no matter how deep they are in the components tree. The context is used to manage...
Read more >
React Context API: What is it and How it works?
How to use Context API? · Create a component that will wrap the provider named Provider e.g. UserProvider. Example using React Hooks: ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found