Make your own React Library

Lee young-jun
3 min readMar 15, 2022

--

Sometimes I forgot how to make event like componentDidMount/componentWillMount in functional component.
So I am using custom hook ‘useUnmount’.
And this time I made a new hook ‘useMount’.
I decided make an open source library with both hooks.
This time I will show you how I created my own React NPM Library.

create-react-library

You should install npm package to create a new npm library.

npm install -g create-react-librarynpx create-react-library

enzyme

If you need tests for UI Testing, use enzyme.

yarn add enzyme enzyme-adapter-react-16yarn add @types/enzyme

useDidMount.test.ts (Example)

describe('useDidMount', () => {  it('Mount handler will can be called', () => {    const renderer = mount(<TestComponent />)    renderer.mount()    expect(componentDidMount).toBeCalled()  })})

‘Const declarations’ require an initialization value

If you get this error when run tests, install babel preset for typescript.

yarn add @babel/preset-typescript

package.json

"babel": {  "presets": [    ...,    "@babel/preset-typescript"  ]}

Support for the experimental syntax ‘jsx’ isn’t currently enabled.

package.json

"babel": {  "presets": [     "@babel/preset-env",     "@babel/preset-react"  ]}

Enzyme expects an adapter to be configured, but found none

setup.ts

import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new Adapter() });

package.json

"jest": {
...
"setupFiles": [
"./__tests__/setup.ts"
]
}

Github

git push — set-upstream origin main

NPM

Create your NPM Account

Login

You would receive OTP email when you try to login npm.

npm login

npm notice Log in on https://registry.npmjs.org/Username: 2semPassword:Email: (this IS public) Enter one-time password from your authenticator app: 41822193
Logged in as 2sem on https://registry.npmjs.org/.

Publish

npm publish

npm noticenpm notice 📦  react-functional-mount@1.0.0npm notice === Tarball Contents ===npm notice 746B  README.mdnpm notice 75B   dist/__tests__/src/useDidMount.test.d.tsnpm notice 75B   dist/__tests__/src/useWillUnmount.test.d.tsnpm notice 142B  dist/index.d.tsnpm notice 391B  dist/index.jsnpm notice 686B  dist/index.js.mapnpm notice 355B  dist/index.modern.jsnpm notice 692B  dist/index.modern.js.mapnpm notice 12B   dist/index.test.d.tsnpm notice 142B  dist/src/index.d.tsnpm notice 75B   dist/useDidMount.test.d.tsnpm notice 2.4kB package.jsonnpm notice === Tarball Details ===npm notice name:          react-functional-mountnpm notice version:       1.0.0npm notice filename:      react-functional-mount-1.0.0.tgznpm notice package size:  2.0 kBnpm notice unpacked size: 5.8 kBnpm notice shasum:        de24bedcf5eb4722ca323a8838d64df8c0040885npm notice integrity:     sha512-ZQ7ieDqt3agy4[...]wNhNzT0ZZAgSQ==npm notice total files:   12npm notice+ react-functional-mount@1.0.0

Install

Install your published npm libary.

yarn add react-functional-mount

yarn add v1.22.10[1/4] 🔍  Resolving packages...[2/4] 🚚  Fetching packages...[3/4] 🔗  Linking dependencies...
...
...
...
└─ react-functional-mount@1.0.0$ husky installhusky - Git hooks installed✨ Done in 9.67s.

NPM Page

You can find your published library immediately on NPM site.

Version Up

If you want publish new version, run this command.
This will modify your package.json automatically.

npm version 1.0.1

v1.0.1

Publish your library again.

npm publish

After 1 day… 43 Downloads. Amazing!

--

--

No responses yet