Skip to content

Commit 0f9c168

Browse files
author
Pavel Uvarov
committed
initial
1 parent d2fbcfd commit 0f9c168

12 files changed

+6652
-1
lines changed

.babelrc.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const { NODE_ENV, BABEL_ENV } = process.env
2+
const cjs = NODE_ENV === 'test' || BABEL_ENV === 'commonjs'
3+
4+
module.exports = {
5+
presets: [
6+
[
7+
'@babel/preset-env',
8+
{
9+
targets: {
10+
esmodules: true,
11+
},
12+
// Use the equivalent of `babel-preset-modules`
13+
bugfixes: true,
14+
modules: false,
15+
loose: true,
16+
},
17+
],
18+
'@babel/preset-typescript',
19+
],
20+
plugins: [
21+
'@babel/transform-react-jsx',
22+
['@babel/plugin-proposal-class-properties', { loose: true }],
23+
['@babel/plugin-proposal-private-methods', { loose: true }],
24+
['@babel/plugin-proposal-private-property-in-object', { loose: true }],
25+
cjs && ['@babel/transform-modules-commonjs'],
26+
[
27+
'@babel/transform-runtime',
28+
{
29+
useESModules: !cjs,
30+
version: require('./package.json').dependencies[
31+
'@babel/runtime'
32+
].replace(/^[^0-9]*/, ''),
33+
},
34+
],
35+
].filter(Boolean),
36+
assumptions: {
37+
enumerableModuleMeta: true,
38+
},
39+
}

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lib
2+
node_modules

.eslintrc

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"parser": "@typescript-eslint/parser",
3+
"extends": [
4+
"eslint:recommended",
5+
"plugin:import/recommended",
6+
"plugin:react/recommended",
7+
"plugin:prettier/recommended"
8+
// "plugin:@typescript-eslint/recommended"
9+
],
10+
"settings": {
11+
"react": {
12+
"version": "detect"
13+
},
14+
"import/ignore": ["react-native"],
15+
"import/resolver": {
16+
"node": {
17+
"extensions": [".js", ".ts", ".tsx"]
18+
}
19+
}
20+
},
21+
"parserOptions": {
22+
"ecmaVersion": 6,
23+
"sourceType": "module",
24+
"ecmaFeatures": {
25+
"jsx": true,
26+
"experimentalObjectRestSpread": true
27+
}
28+
},
29+
"env": {
30+
"browser": true,
31+
"mocha": true,
32+
"node": true
33+
},
34+
"rules": {
35+
"valid-jsdoc": 2,
36+
"react/jsx-uses-react": 1,
37+
"react/jsx-no-undef": 2,
38+
"react/jsx-wrap-multilines": 2,
39+
"react/no-string-refs": 0,
40+
"no-unused-vars": "off",
41+
"@typescript-eslint/no-unused-vars": ["error"],
42+
"no-redeclare": "off",
43+
"@typescript-eslint/no-redeclare": ["error"]
44+
},
45+
"plugins": ["@typescript-eslint", "import", "react"],
46+
"globals": {
47+
"JSX": true
48+
}
49+
}

.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"semi": false,
3+
"singleQuote": true,
4+
"tabWidth": 2,
5+
"endOfLine": "auto"
6+
}

README.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,68 @@
1-
# use-global-state-react
1+
## useGlobalState
2+
3+
**Simple useGlobalState hook for React!**
4+
5+
Do you need your data shared across other components, and you simply don't want to pass props all the way down, create context or use state management tool?
6+
7+
Just use `useGlobalState`, it's really simple and does all the magic for you:
8+
- first argument is a key for your store
9+
- second argument is optional - initial value
10+
11+
### Example:
12+
```ts
13+
import { useGlobalState } from 'use-global-state-react';
14+
15+
const TASK_STORE_KEY = 'tasks';
16+
17+
const Tasks = () => {
18+
const [tasks, setTasks] = useGlobalState<string[]>(TASK_STORE_KEY, []);
19+
20+
...
21+
}
22+
```
23+
24+
and then you can use the same hook everywhere, data will be shared across components and component will rerender if changes happened in store:
25+
26+
```ts
27+
const AddTaskButton = () => {
28+
const [, setTasks] = useGlobalState(TASK_STORE_KEY, []);
29+
30+
const onTaskAdd = (newTask: stirng) => setTasks(prev => [...prev, newTask]);
31+
32+
...
33+
}
34+
35+
const TasksTotal = () => {
36+
const [tasks] = useGlobalState<string[]>(TASK_STORE_KEY, []);
37+
38+
return tasks.length;
39+
}
40+
```
41+
42+
43+
or use helper `createGlobalStore` to create custom hook with shared data:
44+
45+
```ts
46+
import { createGlobalStore } from 'use-global-state-react';
47+
48+
const useTasks = createGlobalStore<string[]>(TASK_STORE_KEY, []);
49+
50+
51+
const Tasks = () => {
52+
const [tasks, setTasks] = useTasks();
53+
...
54+
}
55+
56+
```
57+
58+
## Installation
59+
60+
```
61+
npm i use-global-state-react
62+
```
63+
64+
or
65+
66+
```
67+
yarn add use-global-state-react
68+
```

0 commit comments

Comments
 (0)