Skip to content

Commit 2792811

Browse files
author
John Richard Chipps-Harding
authored
Merge pull request #1 from phantomstudios/feat/initial-component
Initial version with very minimal tests
2 parents 8d22ebc + 226eb9d commit 2792811

File tree

11 files changed

+313
-90
lines changed

11 files changed

+313
-90
lines changed

.github/ISSUE_TEMPLATE/REPORT_A_BUG.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ about: "Is something not working as you expect?"
99
1010
e.g. "Data refuses to load when accessing via https..."
1111

12-
> Which version of PACKAGE-NAME are you using?
12+
> Which version of use-local-state are you using?
1313
1414
e.g. `0.1.0`
1515

16-
> What version of React are you using PACKAGE-NAME within?
16+
> What version of React are you using use-local-state within?
1717
1818
e.g. `16.9.0`
1919

@@ -31,8 +31,8 @@ e.g. "The request just hung, no response."
3131

3232
<!--
3333
Before posting, please check that the bug hasn't already been:
34-
1. fixed in the next release (https://github.com/phantomstudios/PACKAGE-NAME/blob/master/CHANGELOG.md)
35-
2. discussed previously (https://github.com/phantomstudios/PACKAGE-NAME/search)
34+
1. fixed in the next release (https://github.com/phantomstudios/use-local-state/blob/master/CHANGELOG.md)
35+
2. discussed previously (https://github.com/phantomstudios/use-local-state/search)
3636
-->
3737

3838
<!--

.github/ISSUE_TEMPLATE/REQUEST_A_FEATURE.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ e.g. "A new input argument that accepts..."
1515

1616
<!--
1717
Before posting, please check that the feature hasn't already been:
18-
1. added in the next release (https://github.com/phantomstudios/PACKAGE-NAME/blob/master/CHANGELOG.md)
19-
2. discussed previously (https://github.com/phantomstudios/PACKAGE-NAME/search)
18+
1. added in the next release (https://github.com/phantomstudios/use-local-state/blob/master/CHANGELOG.md)
19+
2. discussed previously (https://github.com/phantomstudios/use-local-state/search)
2020
-->
2121

2222
<!--

CONTRIBUTING.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ I'm really happy that you're interested in helping out with this little project.
44

55
As this is very early days for the project there's not a lot in the way of
66
resources, but please check out the [documentation](./README.md), and also the
7-
[list of issues](https://github.com/phantomstudios/PACKAGE-NAME/issues).
7+
[list of issues](https://github.com/phantomstudios/use-local-state/issues).
88

99
Please submit an issue if you need help with anything.
1010

@@ -20,7 +20,7 @@ pass. Testing is automated using GitHub Actions CI.
2020
## Submitting changes
2121

2222
Please send a
23-
[GitHub Pull Request to PACKAGE-NAME](https://github.com/phantomstudios/PACKAGE-NAME/pull/new/master)
23+
[GitHub Pull Request to use-local-state](https://github.com/phantomstudios/use-local-state/pull/new/master)
2424
with a clear list of what you've done (read more about
2525
[pull requests](https://help.github.com/en/articles/about-pull-requests)). When you send a pull
2626
request, please make sure you've covered off all the points in the template.
@@ -43,6 +43,6 @@ In effect this means:
4343
- Your Pull Request title and description become very important; they are the
4444
history of the master branch and explain all the changes.
4545
- You ought to be able to find any previous version easily using GitHub tabs, or
46-
[Releases](https://github.com/phantomstudios/PACKAGE-NAME/releases)
46+
[Releases](https://github.com/phantomstudios/use-local-state/releases)
4747

4848
Thanks, John Chipps-Harding

README.md

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,80 @@
1-
# PACKAGE-NAME
1+
# use-local-state
22

33
[![NPM version][npm-image]][npm-url]
44
[![Actions Status][ci-image]][ci-url]
55
[![PR Welcome][npm-downloads-image]][npm-downloads-url]
66

7-
Package one-liner overview.
7+
A simple react hook that allows you to store data within `LocalStorage` with same interface as the standard `React.useState` hook.
88

99
## Introduction
1010

11-
Package introduction, couple of paragraphs.
11+
Just swap out your `useState` calls with `useLocalState` to persist the data between refreshes. This hook is also SSR safe and does not break when used without `window` existing.
1212

1313
```javascript
14-
import useLibrary from "@phntms/PACKAGE-NAME";
14+
import useLocalState from "@phntms/use-local-state";
1515

16-
const { something } = useLibrary({
17-
argument1: "something",
18-
argument2: "something else",
19-
});
16+
const [token, setToken] = useLocalState("USER_TOKEN", "");
2017
```
2118

2219
## Installation
2320

2421
Install this package with `npm`.
2522

2623
```bash
27-
npm i @phntms/PACKAGE-NAME
24+
npm i @phntms/use-local-state
2825
```
2926

3027
## Usage
3128

32-
Example 1 description.
29+
Store a boolean to track if a user has detected terms of use.
3330

3431
```JSX
3532
import React from 'react';
36-
import useLibrary from '@phntms/PACKAGE-NAME';
33+
import useLocalState from '@phntms/use-local-state';
3734

3835
const SomeExample = () = {
39-
const { something } = useApi({
40-
argument1: "something",
41-
argument2: "something else",
42-
});
36+
const [accepted, setAccepted] = useLocalState("TERMS_ACCEPTED", false);
4337

4438
return (
4539
<>
46-
<h1>Result</h2>
47-
<p>{something}</p>
40+
<h1>Welcome</h2>
41+
{!accepted && <>
42+
<p>Do you accept our terms?</p>
43+
<button onClick={()=>setAccepted(true)}>Accept</button>
44+
</>}
4845
</>
4946
);
5047
}
5148
```
5249
53-
Example 2 description.
50+
Store a list of bookmarks.
5451
5552
```JSX
5653
import React from 'react';
57-
import useLibrary from '@phntms/PACKAGE-NAME';
54+
import useLocalState from '@phntms/use-local-state';
55+
56+
interface Bookmark {
57+
title: string;
58+
url: string;
59+
}
5860

5961
const SomeExample2 = () = {
60-
const { something } = useApi({
61-
argument1: "something",
62-
argument2: "something else",
63-
});
62+
const [bookmarks, setBookmarks] = useLocalState<Bookmark[]>("BOOKMARKS", []);
63+
64+
const addBookmark = (bookmark: Bookmark) => setBookmarks([...bookmarks, bookmark]);
65+
6466

6567
return (
6668
<>
67-
<h1>Result</h2>
68-
<p>{something}</p>
69+
<h1>Bookmarks</h2>
70+
<NewBookmark add={addBookmark} />
71+
<ul>
72+
{bookmarks.map(((bookmark, i) => (
73+
<li key={i}>
74+
<a href={bookmark.url}>{bookmark.title}</a>
75+
</li>
76+
)))}
77+
</ul>
6978
</>
7079
);
7180
}
@@ -75,16 +84,16 @@ const SomeExample2 = () = {
7584
7685
### Input
7786
78-
- `argument1` : Required - Description of argument.
79-
- `argument2` : Optional - Description of argument.
87+
- `key` : Required - The key to store within `LocalStorage`.
88+
- `initialValue` : Required - The default/initial value if the key is not found within the `LocalStorage` object.
8089
8190
### Output
8291
83-
- `something`: Description of output.
92+
An array containing the value and a function to set the value. Signiature is exactly like the standard [React.useState](https://reactjs.org/docs/hooks-state.html) hook.
8493
85-
[npm-image]: https://img.shields.io/npm/v/@phntms/PACKAGE-NAME.svg?style=flat-square&logo=react
86-
[npm-url]: https://npmjs.org/package/@phntms/PACKAGE-NAME
87-
[npm-downloads-image]: https://img.shields.io/npm/dm/@phntms/PACKAGE-NAME.svg
88-
[npm-downloads-url]: https://npmcharts.com/compare/@phntms/PACKAGE-NAME?minimal=true
89-
[ci-image]: https://github.com/phantomstudios/PACKAGE-NAME/workflows/test/badge.svg
90-
[ci-url]: https://github.com/phantomstudios/PACKAGE-NAME/actions
94+
[npm-image]: https://img.shields.io/npm/v/@phntms/use-local-state.svg?style=flat-square&logo=react
95+
[npm-url]: https://npmjs.org/package/@phntms/use-local-state
96+
[npm-downloads-image]: https://img.shields.io/npm/dm/@phntms/use-local-state.svg
97+
[npm-downloads-url]: https://npmcharts.com/compare/@phntms/use-local-state?minimal=true
98+
[ci-image]: https://github.com/phantomstudios/use-local-state/workflows/test/badge.svg
99+
[ci-url]: https://github.com/phantomstudios/use-local-state/actions

SUPPORT.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# PACKAGE-NAME Support
1+
# use-local-state Support
22

3-
For _questions_ on how to use `PACKAGE-NAME` or what went wrong when you tried something, our primary resource is by opening a
4-
[GitHub Issue](https://github.com/phantomstudios/PACKAGE-NAME/issues), where you can get help from developers.
3+
For _questions_ on how to use `use-local-state` or what went wrong when you tried something, our primary resource is by opening a
4+
[GitHub Issue](https://github.com/phantomstudios/use-local-state/issues), where you can get help from developers.

jest.config.js

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,14 @@
22
* @type {jest.ProjectConfig}
33
*/
44
module.exports = {
5-
roots: [
6-
'<rootDir>/test'
7-
],
8-
transform: {
9-
'^.+\\.tsx?$': 'ts-jest'
5+
roots: ["<rootDir>/test"],
6+
transform: {
7+
"^.+\\.tsx?$": "ts-jest",
8+
},
9+
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
10+
globals: {
11+
"ts-jest": {
12+
tsconfig: "tsconfig.test.json",
1013
},
11-
moduleFileExtensions: [
12-
'ts',
13-
'tsx',
14-
'js',
15-
'jsx',
16-
'json',
17-
'node'
18-
],
19-
globals: {
20-
'ts-jest': {
21-
tsConfig: 'tsconfig.test.json'
22-
}
23-
}
24-
}
14+
},
15+
};

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
2-
"name": "@phntms/PACKAGE-NAME",
2+
"name": "@phntms/use-local-state",
33
"description": "Description, same as overview one-liner from README.md.",
44
"version": "0.0.1",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",
7-
"homepage": "https://github.com/phantomstudios/PACKAGE-NAME#readme",
7+
"homepage": "https://github.com/phantomstudios/use-local-state#readme",
88
"repository": {
99
"type": "git",
10-
"url": "https://github.com/phantomstudios/PACKAGE-NAME.git"
10+
"url": "https://github.com/phantomstudios/use-local-state.git"
1111
},
1212
"bugs": {
13-
"url": "https://github.com/phantomstudios/PACKAGE-NAME/issues"
13+
"url": "https://github.com/phantomstudios/use-local-state/issues"
1414
},
1515
"keywords": [
1616
"react",
@@ -67,4 +67,4 @@
6767
"typescript": "^4.1.5"
6868
},
6969
"dependencies": {}
70-
}
70+
}

src/index.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
1-
interface Props {
2-
argument1: number;
3-
argument2?: number;
4-
}
5-
6-
const useLibrary = ({ argument1, argument2 = 0 }: Props) => {
7-
if (!argument1) throw Error("argument 1 not specified");
8-
return {
9-
something: argument1 + argument2,
1+
import { useState } from "react";
2+
3+
import { SUPPORTED } from "./utils";
4+
5+
const useLocalState = <T>(
6+
key: string,
7+
defaultValue: T
8+
): [T, (v: T | ((v: T) => T)) => void] => {
9+
const [value, setValue] = useState<T>(() => {
10+
const toStore =
11+
typeof defaultValue === "function" ? defaultValue() : defaultValue;
12+
if (!SUPPORTED) return toStore;
13+
const item = window.localStorage.getItem(key);
14+
try {
15+
return item ? JSON.parse(item) : toStore;
16+
} catch (error) {
17+
return toStore;
18+
}
19+
});
20+
21+
const setLocalStateValue = (newValue: T | ((v: T) => T)) => {
22+
const isCallable = (value: unknown): value is (v: T) => T =>
23+
typeof value === "function";
24+
const toStore = isCallable(newValue) ? newValue(value) : newValue;
25+
if (SUPPORTED) window.localStorage.setItem(key, JSON.stringify(toStore));
26+
setValue(toStore);
1027
};
28+
29+
return [value, setLocalStateValue];
1130
};
1231

13-
export default useLibrary;
32+
export default useLocalState;

src/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const IS_BROWSER = typeof window !== "undefined";
2+
3+
export const SUPPORTED = IS_BROWSER && window.localStorage;

0 commit comments

Comments
 (0)