react-hookz / web

React hooks done right, for browser and SSR.
https://react-hookz.github.io/web/
MIT License
1.92k stars 92 forks source link

Port remaining hooks from `react-use` #33

Open JoeDuncko opened 3 years ago

JoeDuncko commented 3 years ago

Our goal with @react-hookz/web is to give the react community a general-purpose React hooks library built with care.

We'd like to port the remaining hooks from react-use (the project @react-hookz/web grew out of) while keeping in mind our tenants:

Hooks to port

Sensors

UI

Animations

Side-effects

Lifecycles

State

Miscellaneous

Join our community!

Have a question? Create a discussion on GitHub or join our Discord community.

Interested in contributing code? Check out our contribution guide. We are excited to see your pull request!

xobotyi commented 3 years ago

To be clear - not all hooks will be ported, as for me - i'm against of transfering all create* hooks. And its not the only example.

JoeDuncko commented 3 years ago

To be clear - not all hooks will be ported, as for me - i'm against of transfering all create* hooks. And its not the only example.

Definitely! Should we make a list of all the hooks that we don't want ported? Then I can cross them out above. @xobotyi

xobotyi commented 3 years ago

@JoeDuncko its hard to say rn, im reviewing the hooks on the go and currently refactoring (the full cycle refactoring, meaning reimplementing them from the scratch) hooks im currently using at my job, as this is easiest way for me, as i have little expertise in other areas.

xobotyi commented 3 years ago

useMountedState and useUnmountPromise — track if component is mounted. (Implemented as useIsMounted)

we dont have useUnmountPromise yet

JoeDuncko commented 3 years ago

Crossed out the "create" hooks, split up all the lines with "and"s.

JoeDuncko commented 3 years ago

Added links to the new useIsomorphicLayoutEffect and useDebounceCallback hooks

axelboc commented 3 years ago

Thank you so much for starting this repo and leading this huge migration work! 🍻

Just wondering what the reasoning is for not migrating the create* utilities, and particularly createMemo. In my project, I have a good number of utility functions that loop over very large arrays, so it's important that they are memoised with useMemo. With createMemo I don't have to create hooks with the same signature as the utility functions, so it's really convenient and avoids a lot of duplication (especially since I use TypeScript).

I should add that I can't just turn all of my utility functions into hooks with useMemo inside them, as I use some of them inside loops. I need both the hook and non-hook versions.

JoeDuncko commented 3 years ago

This issue should now be up to date FYI.

hedgerh commented 3 years ago

It'd be nice to know what needs to be done to port a hook over. I'd like to port useCopyToClipboard, but I'm not sure if it needs refactoring.

Should it keep using useSetState? Can useSetState be ported over as-is? It also needs to use useIsMounted instead of useMountedState. What else?

I wouldn't mind helping out with stories/docs so someone with more context could focus on writing/porting the hooks.

xobotyi commented 3 years ago

@hedgerh it can't be ported as-is since it's dependency (clipboardjs) should be listed as optional depency now (look at useCookie to see how package check implemented). Tbh I don't remember all internals and away from pc, I'll reply to you once I'll reach the laptop.

hedgerh commented 3 years ago

No rush. Why are these being listed as peerdeps and as optional now?

xobotyi commented 3 years ago

@hedgerh since very few people need these hooks - there is no need to make everyone to download them.

Rey-Wang commented 3 years ago

@xobotyi @JoeDuncko I'm wondering are going to implement the useCustomCompareEffect |useDeepCompareEffect hook?

xobotyi commented 3 years ago

@Rey-Wang working on it right now, will be published Monday I guess

JoeDuncko commented 2 years ago

This issue is now up to date.

@Rey-Wang we now have useCustomCompareEffect and useDeepCompareEffect!

kylemh commented 2 years ago

usePreviousDistinct done ✅

JoeDuncko commented 2 years ago

usePreviousDistinct done ✅

I think we are missing this one in the README

EDIT: Made a PR

matthew-dean commented 2 years ago

Can you please re-visit removing createMemo? It's an extremely useful pattern that allows you to share a memoized function across components. https://github.com/streamich/react-use/blob/master/docs/createMemo.md

Bailig commented 2 years ago

Hello, I'm first time working on open source projects. I want to start with useError :)

Bailig commented 2 years ago

Hello, I finished the useError hook, but I couldn't start the storybook. I'm just wondering if it's a known issue?

yarn run v1.22.18
$ start-storybook -p 6006 --docs --no-manager-cache
info @storybook/react v6.4.22
info 
info => Loading presets
info => Serving static files from ././.storybook/public at /
ERR! TypeError: (intermediate value) is not iterable
ERR!     at _default (/node_modules/@storybook/builder-webpack4/dist/cjs/preview/iframe-webpack.config.js:73:22)
ERR!     at processTicksAndRejections (internal/process/task_queues.js:95:5)
ERR!     at async Object.start (/node_modules/@storybook/builder-webpack4/dist/cjs/index.js:91:16)
ERR!     at async Promise.all (index 0)
ERR!     at async storybookDevServer (/node_modules/@storybook/core-server/dist/cjs/dev-server.js:126:28)
ERR!     at async buildDevStandalone (/node_modules/@storybook/core-server/dist/cjs/build-dev.js:115:31)
ERR!     at async buildDev (/node_modules/@storybook/core-server/dist/cjs/build-dev.js:161:5)
ERR!  TypeError: (intermediate value) is not iterable
ERR!     at _default (/node_modules/@storybook/builder-webpack4/dist/cjs/preview/iframe-webpack.config.js:73:22)
ERR!     at processTicksAndRejections (internal/process/task_queues.js:95:5)
ERR!     at async Object.start (/node_modules/@storybook/builder-webpack4/dist/cjs/index.js:91:16)
ERR!     at async Promise.all (index 0)
ERR!     at async storybookDevServer (/node_modules/@storybook/core-server/dist/cjs/dev-server.js:126:28)
ERR!     at async buildDevStandalone (/node_modules/@storybook/core-server/dist/cjs/build-dev.js:115:31)
ERR!     at async buildDev (/node_modules/@storybook/core-server/dist/cjs/build-dev.js:161:5)
JoeDuncko commented 2 years ago

@Bailig Yes, Storybook being broken in main is a known issue. Dependabot broke something last week. Will try to fix it today. Sorry about that!

jquintozamora commented 2 years ago

Hey guys, I'd like to do migration from old react-use, the only remaining bit for me is useBeforeUnload. Do you have some estimation when will be migrated ?

thanks!

kylemh commented 2 years ago

This is a relatively easy project to get involved with contributions! Maybe you'd be interested in opening a PR yourself for it?

xobotyi commented 2 years ago

useBeforeUnload is easily achievable with useEventListener hook

useEventListener(window, 'beforeunload', ()=>{ /* do your stuff here */ })
kylemh commented 2 years ago

If we don't plan to implement useBeforeUnload, maybe we should strike it from OP?

xobotyi commented 2 years ago

surely do, just realized just now (list been made up before useEventListener implementation)

xobotyi commented 2 years ago

Overall you can make a PR with migration guide for taht hook (have not enough time for that atm)

kylemh commented 2 years ago

That being said, maybe there's something to be said for NOT porting this hook if it's so easy to accomplish with useEventListener

xobotyi commented 2 years ago

I'll mention it in the list later

xobotyi commented 2 years ago

@ArttuOll could you please make a mention to migration guide about unwilling to implement useBeforeUnload with example from above conversation?

ArttuOll commented 2 years ago

Updated the above list to match #1004 .

axelboc commented 2 years ago

While you're at it, I think useWindowSize can be ticked off ✔️

ArttuOll commented 2 years ago

I also ticked the ones that are not going to be implemented, so that they would also be counted as completed tasks.

ArttuOll commented 2 years ago

What does everybody think about porting of the location-related hooks such as useHash, useLocation and useSearchParam? There are quite a few routing libraries out there that also offer these hooks. Does it make sense for us to offer them as well?

Personally I think no 🤔

kylemh commented 2 years ago

I agree with you.

xobotyi commented 2 years ago

Just been thinking about it yesterday when used react-router's hook😁 Strike them out.

xobotyi commented 2 years ago

Also you can strike out useRaf as it is achievable via composition of useRerender and useRafEffect literally like useRafEffect(useRerender()) It does not provide handles to start and/or stop rerendering therefore it is not the full replacement. Better to finish the PR that we have.

ArttuOll commented 1 year ago

I presume useUpsert will not be ported, since useList already provides an upsert function. I ticked it on the list and created a PR (#1046) to update the migration guide.

jpwallace22 commented 1 year ago

Unless I'm missing something here, I feel like useSize can probably be crossed off. Since there is a useMeasure and useResizeObserver

jpwallace22 commented 1 year ago

@ArttuOll I was looking at porting over useTimeout and useTimeoutFn next. useTimeout is pretty much just useRerender on a timer. Do you think it would be best to make that hook? Or what are your thoughts on just having an optional timer on useRerender?

xobotyi commented 1 year ago

I can't see the sense of porting useTimeout but useTimeoutCallback would be pretty much handy one, since it could be well composed with other hooks

jpwallace22 commented 1 year ago

@xobotyi I was about to do it, but it looks like there is already a useTimeoutEffect that does it. Might want to check the box.

ArttuOll commented 1 year ago

Updated the list again.

ArttuOll commented 1 year ago

Ticked off usePromise, which will not be implemented, as decided in #1048 . Also ticked off useLogger, which has been implemented as useLifecycleLogger and useRendersCount which has been implemented as useRenderCount.

What does everybody think about porting of the following hooks:

xobotyi commented 1 year ago

agreed on all of them

tansanDOTeth commented 1 year ago

For useTween, I would like to make a suggestion to the API. It would be helpful if it can return a start function to grant control over when to start the tweening process.

const [t, start, stop, restart] = useTween()
Frankcaozas commented 1 year ago

@JoeDuncko can i work on useScrolling, only the test is not done

JoeDuncko commented 1 year ago

@JoeDuncko can i work on useScrolling, only the test is not done

Sure!

malavshah9 commented 1 year ago

@JoeDuncko I would like to work on useQueue, can I start working on it?

JoeDuncko commented 1 year ago

@JoeDuncko I would like to work on useQueue, can I start working on it?

Sure!