Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hook-use-url

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hook-use-url

Easy manage of URL state withing ReactJS app with react-router v6

  • 2.0.1
  • npm
  • Socket score

Version published
Weekly downloads
11
increased by10%
Maintainers
1
Weekly downloads
 
Created
Source

npm i hook-use-url

Tested with react-router v6 and ReactJS v18

Overview

import useUrl from 'hook-use-url';

function MyComponent() {
  const url = useUrl();

  // get current url
  url.get(); // https://example.com/some-path?a=b&arr=x&arr=y
  // get key's value from URL. Works with arrays too.
  const a = url.get('a'); // b
  const arr = url.get('arr'); // ["x", "y"]

  // update a to c & update URL & update browser's history with history.push
  url.set({key: 'a', value: 'c'}).push();
  // update a to c & update URL & update browser's history with history.replace
  url.set({key: 'a', value: 'c'}).replace();
  // update a to c & return new URL without changing it and without changing browser's history
  url.set({key: 'a', value: 'c'}).get();
}

API

  • You can chain several actions like so: url.set("x1", "foo").set("x2", "foo").push()
  • To update browser's URL & history after changes - use .push() or .replace()
  • When getting value, if it's not available, undefined is returned.
FunctionDescriptionSample Output
url.getUri()Get full URIhttps://example.come/some-path?x1=b#foo
url.getPath()Get path/some-path
url.getQuery()Get queryx1=b
url.getFragment()Get fragmentfoo, undefined
url.getDomain()Get domainexample.com
url.getProtocol()Get protocolhttps
url.get("x1")Get value of x1b
url.set("x1", "c").getUri()Set value of x1 to c and get new uri.https://example.come/some-path?x1=c#foo
url.set("x1", ["c", "d"]).getQuery()Set value of x1 and get new query.x1=c&x1=d
url.add("x1", ["e"])If x1 already has some value, add one more value. Otherwise just set x1 to eurl object to chain more methods.
url.set("x1")Set x1 value to undefined. Does same as url.remove("x1")url object to chain more methods.
url.remove("x1")Remove x1 value if there is any.url object to chain more methods.
url.removeQuery()Remove query from URIurl object to chain more methods.
url.removeFragment()Remove fragment from URIurl object to chain more methods.
url.removePath()Remove path from URIurl object to chain more methods.
url.push()Set URI at browser's address input and push history event to browser's historyundefined
url.replace()Set URI at browser's address input and replace current history event with new at browser's historyundefined

FAQs

Package last updated on 27 Jun 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc