react-detect-offline icon indicating copy to clipboard operation
react-detect-offline copied to clipboard

Use React context with provider/consumer to pass status

Open ramyma opened this issue 5 years ago • 2 comments

I am using the Online and Offline components in a couple of places within my application and noticed that the application is hitting the polling url every 5 seconds about 3 times, which shows that each Online/Offline component checks separately for the connection's status.

I propose creating a Provider which could be used to wrap the application's root component and just use a Consumer anywhere where the connection status is needed.

It would look something like:

<Provider {...config}>
  <App />
</Provider>
<Consumer>
  {({ online }) => (
    <div>
      {`${online}`}
    </div>
  )}
</Consumer>

I can create a PR to implement it if it's OK.

What do you think ?

ramyma avatar Jan 14 '19 19:01 ramyma

I love the idea of having one provider! Definitely put up the PR and I can take a look at it. Thank you!

chrisbolin avatar Jan 20 '19 04:01 chrisbolin

Hopefully something we can do in v3

chrisbolin avatar Mar 28 '19 03:03 chrisbolin