DEV Community

Cover image for Publishing a React Hooks Library using Typescript and TSDX

Publishing a React Hooks Library using Typescript and TSDX

Julian Garamendy on May 15, 2019

I started my personal hooks library and decided to publish it as an npm package. Doing this in TypeScript was not straightforward, until: palmerhq/...
Collapse
 
devhammed profile image
Hammed Oyedele

I will prefer to use create-react-hook instead because it will setup an example folder that you can deploy to GitHub pages. I once use it for a React hook for manipulating browser cookies.

GitHub logo devhammed / use-cookie

Get, Set, Update and Delete Cookie using React Hooks.

@devhammed/use-cookie

Get, Set, Update and Delete Cookie using React Hooks.

NPM JavaScript Style Guide

Install

npm install --save @devhammed/use-cookie

Usage

import React from 'react'
import ReactDOM from 'react-dom'
import useCookie from '@devhammed/use-cookie'
const App = () => {
  const [username, setUsername, deleteUsername] = useCookie('username', 'User')
  return (
    <section&gt
      <h1>Hello {username}!</h1&gt
      <p>Edit below input, Your name will be stored in a cookie. you can refresh this page to see how it persists.</p&gt
      <input
        type='text'
        value={username}
        onChange={(e) => setUsername(e.target.value)}
      />
      <button
        onClick={() => deleteUsername()}
      >
        Delete Cookie
      </button>
    </section>
  )
}

ReactDOM.render(<App />, 

GitHub logo Hermanya / create-react-hook

🎣CLI for easily creating reusable react hooks.

create-react-hook

CLI for creating reusable React hooks using Rollup and create-react-app Inspired by the amazing create-react-library

NPM Build Status JavaScript Style Guide

How and why I made this tool.

Features

  • Easy-to-use CLI
  • Handles all modern JS features
  • Bundles cjs and es module formats
  • create-react-app for example usage and local dev
  • Rollup for bundling
  • Babel for transpiling
  • Jest for testing
  • Supports complicated peer-dependencies
  • Optional support for TypeScript
  • Sourcemap creation

Install

This package requires node >= 4, but we recommend node >= 8.

npm install -g create-react-hook

Creating a New Hook

create-react-hook

Answer some basic prompts about your module, and then the CLI will perform the following steps:

  • copy over the template
  • install dependencies via yarn or npm
  • link packages together for local development
  • initialize local git repo

Development

Local development is broken into two parts (ideally using two tabs).

First, run rollup to watch your src/ module and automatically recompile it into dist/ whenever you…

Collapse
 
juliang profile image
Julian Garamendy

Awesome! I didn't know create-react-hook. Thank you!

Collapse
 
petyosi profile image
Petyo Ivanov

Had the same issue while developing react-virtuoso with tsdx. Came up with a very crude hack, described in this issue.

Ultimately, however, I switched to storybook - it lives inside the project directory and works with the same React instance.

Collapse
 
juliang profile image
Julian Garamendy • Edited

Thank you! I can't believe I haven't tried storybook yet.

Collapse
 
mlaopane profile image
Mickaël

Thanks for the article.

I'm dealing with the same issue except it happens even when publicly deployed on npm.

I've been using Parcel but I may give a try to create-react-hook instead