First of all, thanks for contributing to @react-hookz
! The collective developing and
using this library appreciates your efforts.
If you are contributing for the first time, we recommend reading this First Contributions guide first.
- Fork the main repo
- Clone the repo to your computer (add
--depth=1
togit clone
command to save time) - Change folder to the cloned repo:
cd ./web
- Install dependencies:
yarn
- Make sure everything builds and tests:
yarn build && yarn test
- Create a branch for your PR:
git checkout -b pr/my-awesome-hook
- if you are adding a new hook, name the branch based on the hook:
pr/useUpdateEffect
- if your change fixes an issue, name the branch based on the issue number:
pr/fix-12345
- if you are adding a new hook, name the branch based on the hook:
- Follow the directions below:
Tip: to keep your
master
branch pointing to the original repo'smaster
(instead of your fork'smaster
) do this:git remote add upstream https://github.com/react-hookz/web.git git fetch upstream git branch --set-upstream-to=upstream/master masterAfter running these commands you'll be able to easily pull changes from the original repository with
git pull
.
- Perform self-check on hook usefulness. We're not interested in hooks that has too specific usecase or hooks that can be easily achieved by composition of existing hooks.
- Implement the hook in the
src
folder.- The file with hook implementation should be named
index.ts
and placed in a subdirectory named after the hook. - The hook should have return types explicitly defined.
- The hook should have a JSDoc comment containing a description of the hook and an overview of its arguments.
- The hook should be exported by name, not default-exported.
- If the hook has custom types in its parameters or return values, they should be exported as well.
- Types and interfaces should not have prefixes like
I
orT
. - The hook should be developed with SSR in mind, meaning that usage of hook in SSR environment should not lead to errors.
- If your hook reuses other @react-hookz/web hooks, import them as
import { useToggle } from '../useToggle';
instead ofimport { useToggle } from '..';
- The file with hook implementation should be named
- Re-export the hook implementation and all its custom types in
src/index.ts
. - Fully test your hook. The tests should include tests for both DOM and SSR environments.
- Hook's tests should be placed in
__tests__
subdirectory, next to the source file -dom.ts
for DOM environment,ssr.ts
for SSR environment.
For example:src/useFirstMountState/__tests__/dom.ts
andsrc/useFirstMountState/__tests__/ssr.ts
. - Ideally, your hook should have 100% test coverage. If that is impossible, you should leave a comment in the code describing why.
- Each hook should have at least
'should be defined'
and'should render'
tests inSSR
environment. - All utility functions should also be tested.
- Hook's tests should be placed in
- Write docs for your hook.
- Docs should be placed in
__docs__
sub-folder, near the source file.
For example:src/useFirstMountState/__docs__/story.mdx
. - Docs are built with Storybook. You can run
yarn storybook:watch
to preview your work. - Write a short example demonstrating your hook in
example.stories.tsx
within the__docs__
folder. (If the filename misses the.stories.tsx
part, Storybook won't find your example.)
For example:src/useFirstMountState/__docs__/example.stories.tsx
. - Docs are written in MDX format.
Read more about storybook docs.
- Docs should be placed in
- Add a summary of the hook and a link to the docs to
README.md
. - After all the above steps are done, run
yarn lint:fix
to ensure that everything is styled by our standards and there are no linting issues. yarn new-hook myAwesomeHook
will help you to create a proper file structure for the new hook.
- Check from #33 and the migration guide that the hook has been approved for porting. If there is no previous discussion on the hook in #33, leave a comment there asking if you could port the hook. In your comment, provide a valid use-case for the hook.
- Don't just copy-paste the hook. Think through the code:
- Is there sufficient tests?
- Could the hook be implemented by reusing existing hooks in
@react-hookz/web
? - Is the documentation exhaustive?
- Is the example useful?
This repo uses semantic-release and
conventional commit messages so prefix your commits with fix:
,
feat:
, etc., so that your changes appear in the
release notes.
Also, there is a script that helps you to properly format commit messages:
git add .
yarn commit
The script guides you through several prompts that help you with writing a good commit message, including many non-obvious and easy-to-forget parts.
This project includes Git hooks that are automatically enabled when you install the dependencies. These hooks automatically test and validate your code and commit messages before each commit. In most cases disabling these hooks is not recommended.