Skip to content

Commit fa165a5

Browse files
authored
Merge pull request #18 from alevnyacow/feature/tests-and-rewrite-whole
version 2.0.0
2 parents 70950f6 + 3eed6fd commit fa165a5

23 files changed

+385
-45
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
package-lock.json
3-
transpiled
3+
transpiled
4+
*.tgz

README.md

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@
99

1010
## **About**
1111

12-
Shared React variables provide you easy and comfortable mutable state management based on hooks. It's extremely simple - you generate a hook in one line and then you can use it anywhere to operate with your state! Also you can have any number of such state variables in your application.
12+
Shared React variables provide you easy and comfortable mutable state management based on hooks.
13+
14+
## 🔍 **Example**
1315

1416
```tsx
15-
// creating timer state hook
16-
const useTimer = createUseSharedVariable({ ticks: 0 });
17+
const [
18+
// returns a mutable state
19+
useTimer,
20+
// use it to rewrite the whole variable,
21+
// normally you don't need it
22+
useTimerRewrite
23+
] = createUseSharedVariable({ ticks: 0 });
1724

1825
const Timer = () => {
1926
// using timer state
@@ -36,36 +43,6 @@ const AnotherTimerWithSameState = () => {
3643
};
3744
```
3845

39-
## 📖 **API**
40-
41-
### **1. createUseSharedVariable**
42-
#### Description
43-
Takes initial state as a parameter. Returns a hook can be imported and used anywhere in your application. When you use this hook, it returns you mutable global state. Whenever this state is changed component will be rerendered (this behaviour can be changed via *rerenderOnChange* flag in the hook).
44-
45-
#### Signature
46-
```ts
47-
<T extends object>(initialState: T) => ReactVariableHook<T>;
48-
```
49-
50-
#### Usage example
51-
```ts
52-
// feel free to use this state hook anywhere!
53-
const useTimer = createUseSharedVariable({ ticks: 0 });
54-
55-
export { useTimer }
56-
```
57-
58-
59-
## 🔍 **Exported types**
60-
61-
### **1. ReactVariableHook**
62-
#### Description
63-
Describes signature of a hook which returns *createUseSharedVariable* method.
64-
#### Signature
65-
```ts
66-
type ReactVariableHook<T> = (rerenderOnChange?: boolean) => T;
67-
```
68-
69-
## 💡 **Package usage example**
46+
## 💡 **Codesandbox**
7047

7148
https://codesandbox.io/s/react-shared-variables-example-f7feo7

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "@alevnyacow/shared-react-variables",
3-
"version": "1.0.4",
3+
"version": "2.0.0",
44
"description": "Easy and comfortable React state manager",
55
"main": "transpiled/index.js",
66
"scripts": {
7-
"test": "jest",
7+
"test": "npm run build && npm pack && cd ./tests/test-application && npm i && npm run test",
88
"clean_transpiled": "del-cli --force ./transpiled",
99
"format": "prettier --config .prettierrc --write sources/**",
1010
"lint": "eslint sources/**/*.ts --ext .ts",

sources/index.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@ import { useRerenderer } from "./use-rerenderer";
55
import { rerenderersList } from "./rerenderers-list";
66

77
type ReactVariableHook<T> = (rerenderOnChange?: boolean) => T;
8+
type ReactVariableRewriteHook<T> = () => (
9+
generator: (oldState: T) => T
10+
) => void;
811

9-
function createUseSharedVariable<T extends object>(
10-
initialState: T
11-
): ReactVariableHook<T> {
12+
function createUseSharedVariable<T extends object>(initialState: T) {
1213
const variableIdentifier = v4();
13-
const sharedVariable = deepProxy(
14+
let sharedVariable = deepProxy(
1415
[rerenderersList.fire],
1516
variableIdentifier
1617
)(initialState);
1718

18-
function useSharedVariable(rerenderOnChange = true) {
19+
const useSharedVariable: ReactVariableHook<T> = (
20+
rerenderOnChange = true
21+
) => {
1922
const rerenderer = useRerenderer();
2023
const memoizedRerenderer = useCallback(rerenderer, []);
2124
const rerenderIdentifier = useRef(v4());
@@ -34,9 +37,18 @@ function createUseSharedVariable<T extends object>(
3437
}, []);
3538

3639
return sharedVariable;
37-
}
40+
};
3841

39-
return useSharedVariable;
42+
const useSharedVariableRewrite: ReactVariableRewriteHook<T> =
43+
() => (generator: (oldState: T) => T) => {
44+
sharedVariable = deepProxy(
45+
[rerenderersList.fire],
46+
variableIdentifier
47+
)(generator(sharedVariable)) as T;
48+
rerenderersList.fire(variableIdentifier);
49+
};
50+
51+
return [useSharedVariable, useSharedVariableRewrite] as const;
4052
}
4153

42-
export { createUseSharedVariable, ReactVariableHook };
54+
export { createUseSharedVariable, ReactVariableHook, ReactVariableRewriteHook };

tests/test-application/.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*

tests/test-application/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Getting Started with Create React App
2+
3+
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4+
5+
## Available Scripts
6+
7+
In the project directory, you can run:
8+
9+
### `npm start`
10+
11+
Runs the app in the development mode.\
12+
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
13+
14+
The page will reload if you make edits.\
15+
You will also see any lint errors in the console.
16+
17+
### `npm test`
18+
19+
Launches the test runner in the interactive watch mode.\
20+
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21+
22+
### `npm run build`
23+
24+
Builds the app for production to the `build` folder.\
25+
It correctly bundles React in production mode and optimizes the build for the best performance.
26+
27+
The build is minified and the filenames include the hashes.\
28+
Your app is ready to be deployed!
29+
30+
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31+
32+
### `npm run eject`
33+
34+
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
35+
36+
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37+
38+
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
39+
40+
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
41+
42+
## Learn More
43+
44+
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45+
46+
To learn React, check out the [React documentation](https://reactjs.org/).
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "test-application",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"@alevnyacow/shared-react-variables": "file:../../alevnyacow-shared-react-variables-2.0.0.tgz",
7+
"@testing-library/jest-dom": "^5.16.2",
8+
"@testing-library/react": "^12.1.4",
9+
"@testing-library/user-event": "^13.5.0",
10+
"@types/jest": "^27.4.1",
11+
"@types/node": "^16.11.26",
12+
"@types/react": "^17.0.40",
13+
"@types/react-dom": "^17.0.13",
14+
"react": "^17.0.2",
15+
"react-dom": "^17.0.2",
16+
"react-scripts": "5.0.0",
17+
"typescript": "^4.6.2",
18+
"web-vitals": "^2.1.4"
19+
},
20+
"scripts": {
21+
"start": "react-scripts start",
22+
"build": "react-scripts build",
23+
"test": "react-scripts test",
24+
"eject": "react-scripts eject"
25+
},
26+
"eslintConfig": {
27+
"extends": [
28+
"react-app",
29+
"react-app/jest"
30+
]
31+
},
32+
"browserslist": {
33+
"production": [
34+
">0.2%",
35+
"not dead",
36+
"not op_mini all"
37+
],
38+
"development": [
39+
"last 1 chrome version",
40+
"last 1 firefox version",
41+
"last 1 safari version"
42+
]
43+
}
44+
}
3.78 KB
Binary file not shown.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta name="theme-color" content="#000000" />
8+
<meta
9+
name="description"
10+
content="Web site created using create-react-app"
11+
/>
12+
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
13+
<!--
14+
manifest.json provides metadata used when your web app is installed on a
15+
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
16+
-->
17+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
18+
<!--
19+
Notice the use of %PUBLIC_URL% in the tags above.
20+
It will be replaced with the URL of the `public` folder during the build.
21+
Only files inside the `public` folder can be referenced from the HTML.
22+
23+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
24+
work correctly both with client-side routing and a non-root public URL.
25+
Learn how to configure a non-root public URL by running `npm run build`.
26+
-->
27+
<title>React App</title>
28+
</head>
29+
<body>
30+
<noscript>You need to enable JavaScript to run this app.</noscript>
31+
<div id="root"></div>
32+
<!--
33+
This HTML file is a template.
34+
If you open it directly in the browser, you will see an empty page.
35+
36+
You can add webfonts, meta tags, or analytics to this file.
37+
The build step will place the bundled scripts into the <body> tag.
38+
39+
To begin the development, run `npm start` or `yarn start`.
40+
To create a production bundle, use `npm run build` or `yarn build`.
41+
-->
42+
</body>
43+
</html>
5.22 KB
Loading

0 commit comments

Comments
 (0)