npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@payello-ui/react-country

v1.20240420.36

Published

Country detection for React

Downloads

7

Readme

@payello-ui/react-country

A lightweight country detection context for React and React Native applications, with no external dependencies. This package allows you to easily handle and manipulate country data within your application through a React context. It includes functionalities for setting and retrieving the current country, detecting the country automatically based on various signals, and providing a list of permitted countries.

Installation

npm install @payello-ui/react-country
# or
yarn add @payello-ui/react-country

Usage

Setting Up

First, wrap your main component or the specific part of your app that requires country context with CountryProvider:

import React from 'react';
import { CountryProvider } from '@payello-ui/react-country';

function App() {
  return (
    <CountryProvider>
      {/* Rest of your app components */}
    </CountryProvider>
  );
}

export default App;

Accessing and Setting the Country

Inside any component wrapped by CountryProvider, you can access and set the current country using the useCountry hook:

import React from 'react';
import { useCountry } from '@payello-ui/react-country';

function MyComponent() {
  const [country, setCountry] = useCountry();

  return (
    <div>
      <p>Current country: {country}</p>
      <button onClick={() => setCountry('US')}>Set to US</button>
    </div>
  );
}

export default MyComponent;

Country Detection

This package provides a utility to attempt automatic detection of the user's country based on the request information. This can include query parameters, cookies, hostname's top-level domain, CloudFlare headers, and accepted languages header from the browser.

import { requestCountry } from '@payello-ui/react-country';

async function detectCountry(request) {
  const country = await requestCountry(request, {
    // Optional configuration
  });
  console.log(country); // Outputs the detected country code or null
}

// Example usage with a fetch event in a service worker or Cloudflare Worker
self.addEventListener('fetch', (event) => {
  event.respondWith(detectCountry(event.request));
});

The requestCountry function supports a variety of options for detecting the country, allowing you to customize the detection process to fit your needs.

API

Below are the main exports of @payello-ui/react-country:

  • CountryProvider: A React context provider for wrapping your application or parts of it.
  • useCountry: A React hook for accessing and setting the current country within components wrapped by CountryProvider.
  • requestCountry: A function for detecting the country based on a request, useful for server-side rendering or API endpoints.
  • CountryManager: Contains the list of all permitted countries and initial default country.
  • extractCookieValue: Utility function for extracting a value from a cookie string.
  • getBrowserLanguages: Utility function for parsing the browser's Accept-Language header.

Contributing

We welcome contributions to make this package better. If you find a bug or would like a new feature, feel free to open an issue or create a pull request.

License

This package is licensed under the MIT License. See LICENSE file for details.