5 ms·
Show HN: A Simple LocalStorage Hook in React
- r0rshrk 7y agoIt has been a fun journey implementing hooks in React. Let me know your thoughts on the code :)
- modarts 7y ago“Journey” is a bit of a stretch here, no? :) This is a few lines of pretty trivial code
- TomMarius 7y agoMaybe he meant more hooks, in general. For me that is true, hooks really are a very inspirational pattern
- wolco 7y agoGetting the tooling up and running took awhile.
- buildawesome 7y agoMust've been pretty hard to type `npx create-react-app my-app`
- dang 7y agoPlease don't be a jerk on HN, regardless of how correct you are or feel you are. https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html
- buildawesome 7y agoSorry Dang and Wolco.
- deleted 7y ago[deleted]
- dang 7y agoCan you please review the Show HN guidelines? I appreciate that you're not exactly flaming the OP here, but still, the idea is to respond more charitably to people's work. If you know more than others, find a constructive way to express that, such as by teaching them—and by extension, the rest of us—something related to what they've done so far. https://news.ycombinator.com/showhn.html https://news.ycombinator.com/showhn.html https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html
- modarts 7y agoThis is a great point, and on a second reading my comment did come off as a little insensitive -- will leave it up to give others context and make this a teachable moment for all of us to improve the level of discourse :)
- ljoshua 7y agoHey congrats! People will complain about the package being to small or trivial, but just keep on creating. We all start somewhere, and hooks are a fun thing to experiment with. It's true that most folks probably would do their own implementation of this and the GPL license would be a non-starter for most projects, but if it suits your needs and you've learned something in the process, more power to you. :)
- funemployed 7y agobrowser compatibility/other edge case handling would be one decent reason to utilize a package for otherwise straightforward implementations. The MDN docs have a helpful walkthrough for feature detecting localStorage that would make a useful addition to your library. https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage...
- r0rshrk 7y agoOoh, thanks. Must have a look
- vhakulinen 7y agoIs the react hook code so complicated that when it totals up to 17 lines of code[1], it needs to become a npm package? The code it self looks neat and clean - even more reason to just write your own hook. 1: https://github.com/akash-joshi/local-storage-hook/blob/7d5933b8b06c965480ec3f2d2ccd911a595191a1/src/index.js https://github.com/akash-joshi/local-storage-hook/blob/7d593...
- r0rshrk 7y agoExactly, it can be treated as an example. It's an npm package so that I can reuse the code :)
- darknoon 7y agoIt is a difficult to recommend a small package that is GPLv3, since your entire app would have to be licensed appropriately (since it runs in the same process). For projects like Blender, where development is a self-reinforcing ecosystem I appreciate what the GPL does, here not so much.
- crtlaltdel 7y agoi suspect the choice of GPLv3 was not well informed, given the nature of the code in question.
- mdtusz 7y agoMirroring other comments, this seems a bit trivial to be its own package and looking at the source, it seems to not cover cases where the local storage value can't be parsed. https://usehooks.com/useLocalStorage/ https://usehooks.com/useLocalStorage/ has a safer implementation and is free to copy (Unlicense).
- brudgers 7y agoIt seems less trivial than PadLeft. https://arstechnica.com/information-technology/2016/03/rage-quit-coder-unpublished-17-lines-of-javascript-and-broke-the-internet/ https://arstechnica.com/information-technology/2016/03/rage-...
- crtlaltdel 7y agoas a joke i got a shirt with left-pad's source printed on it. #neverforget
- quickthrower2 7y agoI was playing with String.prototype.padLeft earlier and boy that has weird semantics. For the padding length, strings are parsed. NaN is 0, Infinity throws an error, negative Infinity is 0, true is 1. Good ol’ JS!
- sktrdie 7y agoAlso not even test cases for such simple code.
- Izkata 7y agoI feel like the "it's a bit small for a package" comments have forgotten how js-land is going... For example, check out is-number: https://www.npmjs.com/package/is-number https://www.npmjs.com/package/is-number
- bluetidepro 7y ago> I feel like the "it's a bit small for a package" comments have forgotten how js-land is going... For example, check out is-number: https://www.npmjs.com/package/is-number https://www.npmjs.com/package/is-number "Two wrongs don't make a right"
- jbaudanza 7y agoI have noticed that, in the wild, referencing window.localStorage at all will raise a security exception in some browsers. If this happens inside of a React hook, it will unmount everything up to the nearest componentDidCatch(). In practice, everything involving localStorage needs to make liberal use of try, catch blocks.
- andrew_ 7y agoSeconded. localStorage can bite in bad ways, and the unmounting concerns are valid.
- deleted 7y ago[deleted]
- andrew_ 7y agoGPLV3 is an _interesting_ choice for a license for such a small amount of code (17 lines [0]) [0] https://github.com/akash-joshi/local-storage-hook/blob/master/src/index.js https://github.com/akash-joshi/local-storage-hook/blob/maste...