Giter Site home page Giter Site logo

react-shorten-url's Introduction

NPM version NPM downloads NPM license Codecov Travis Bundle size

About

Bitly implementation to React hook

Demo

Playground โ€“ play with the library in CodeSandbox

Similar Projects

How to Install

First, install the library in your project by npm:

$ npm install react-shorten-url

Or Yarn:

$ yarn add react-shorten-url

Getting Started

ShortenUrlProvider

Config Param Values

Name Type Default Description
accessToken string Bitly access token
options BitlyConfig {} Additional Bitly config

useShortenUrl

Options

Name Type Default Description
url string URL to shorten

Returned Values

Name Type Description
loading boolean Is data loading
error Error Error shortening URL
data BitlyLink Data returned from Bitly

Example

โ€ข Import ShortenUrlProvider from library in your React app, wrap main component and set config values:

// index.js

import React from 'react';
import ReactDOM from 'react-dom';
import { ShortenUrlProvider } from 'react-shorten-url';

import App from './App';

ReactDOM.render(
  <ShortenUrlProvider config={{ accessToken: 'bitly_access_token' }}>
    <App />
  </ShortenUrlProvider>,
  document.getElementById('root')
);

โ€ข Then use useShortenUrl Hook:

// App.js

import React from 'react';
import { useShortenUrl } from 'react-shorten-url';

const App = () => {
  const { loading, error, data } = useShortenUrl('https://example.com/');

  if (loading) return <p>Loading...</p>;

  if (error) return <p>Something went wrong</p>;

  return <h1>{data.link}</h1>;
};

export default App;

License

This project is licensed under the MIT License ยฉ 2020-present Jakub Biesiada

react-shorten-url's People

Contributors

jb1905 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

react-shorten-url's Issues

Suggestion: don't try to fetch link if URL is undefined / blank

I've got a situation where I need to generate a link first before shortening it. What I'm doing is storing the URL via useState, so when that value changes the hook generates the Bit.ly link. Works great except initially my URL is undefined so I get a Bit.ly error that shows up the first time the component loads. Not the end of the world but it's an extra API hit and an error message in the console.

I'd love to be able to conditionally use the hook - basically have it not bother to contact Bit.ly if the provided URL is undefined or a blank string, and only actually make the contact if there's a URL to shorten. I can probably write my own custom wrapper but it'd be cool if react-shorten-url just did it by default.

Otherwise, everything works great. Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.