Skip to content

Commit 8821442

Browse files
committed
Initial commit
0 parents  commit 8821442

32 files changed

+2280
-0
lines changed

.eslintrc.js

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
module.exports = {
2+
env: {
3+
node: true
4+
},
5+
parserOptions: {
6+
ecmaVersion: 2019,
7+
sourceType: 'module'
8+
},
9+
globals: {
10+
Promise: true
11+
},
12+
rules: {
13+
'for-direction': 'error',
14+
'getter-return': 'error',
15+
'no-async-promise-executor': 'error',
16+
'no-await-in-loop': 'warn',
17+
'no-compare-neg-zero': 'error',
18+
'no-cond-assign': 'error',
19+
'no-constant-condition': 'error',
20+
'no-debugger': 'error',
21+
'no-dupe-args': 'error',
22+
'no-dupe-keys': 'error',
23+
'no-duplicate-case': 'error',
24+
'no-empty': 'error',
25+
'no-empty-character-class': 'error',
26+
'no-ex-assign': 'error',
27+
'no-extra-boolean-cast': 'error',
28+
'no-extra-parens': ['error', 'all', { returnAssign: false, nestedBinaryExpressions: false, ignoreJSX: 'multi-line' }],
29+
'no-extra-semi': 'error',
30+
'no-func-assign': 'error',
31+
'no-inner-declarations': 'error',
32+
'no-irregular-whitespace': ['error', { skipRegExps: true }],
33+
'no-obj-calls': 'error',
34+
'no-regex-spaces': 'error',
35+
'no-sparse-arrays': 'error',
36+
'no-template-curly-in-string': 'warn',
37+
'no-unexpected-multiline': 'error',
38+
'no-unreachable': 'error',
39+
'no-unsafe-finally': 'error',
40+
'no-unsafe-negation': 'error',
41+
'require-atomic-updates': 'error',
42+
'use-isnan': 'error',
43+
'valid-typeof': 'error',
44+
45+
46+
47+
'array-callback-return': 'error',
48+
'class-methods-use-this': 'error',
49+
'consistent-return': 'error',
50+
'curly': ['error', 'multi-line'],
51+
'dot-location': ['error', 'property'],
52+
'dot-notation': 'error',
53+
'eqeqeq': ['error', 'always'],
54+
'guard-for-in': 'error',
55+
'no-alert': 'error',
56+
'no-caller': 'error',
57+
'no-empty-function': 'error',
58+
'no-empty-pattern': 'error',
59+
'no-eq-null': 'error',
60+
'no-eval': 'error',
61+
'no-extend-native': 'error',
62+
'no-fallthrough': 'error',
63+
'no-floating-decimal': 'error',
64+
'no-global-assign': 'error',
65+
'no-implicit-coercion': ['error', { boolean: false }],
66+
'no-implied-eval': 'error',
67+
'no-invalid-this': 'error',
68+
'no-iterator': 'error',
69+
'no-labels': 'error',
70+
'no-lone-blocks': 'error',
71+
'no-multi-spaces': 'error',
72+
'no-multi-str': 'error',
73+
'no-new': 'error',
74+
'no-new-func': 'error',
75+
'no-new-wrappers': 'error',
76+
'no-octal': 'error',
77+
'no-proto': 'error',
78+
'no-redeclare': 'error',
79+
'no-return-assign': ['error', 'always'],
80+
'no-return-await': 'error',
81+
'no-script-url': 'error',
82+
'no-self-assign': 'error',
83+
'no-self-compare': 'error',
84+
'no-sequences': 'error',
85+
'no-throw-literal': 'error',
86+
'no-unused-labels': 'error',
87+
'no-useless-concat': 'error',
88+
'no-useless-escape': 'error',
89+
'no-useless-return': 'error',
90+
'no-void': 'error',
91+
'no-with': 'error',
92+
'radix': 'error',
93+
'require-await': 'warn',
94+
'wrap-iife': ['error', 'inside'],
95+
'yoda': ['error', 'never', { exceptRange: true }],
96+
97+
98+
99+
'no-delete-var': 'error',
100+
'no-label-var': 'error',
101+
'no-shadow': 'error',
102+
'no-shadow-restricted-names': 'error',
103+
'no-undef': 'error',
104+
'no-undef-init': 'error',
105+
'no-undefined': 'error',
106+
'no-unused-vars': 'warn',
107+
'no-use-before-define': ['error', { functions: false }],
108+
109+
110+
111+
'callback-return': 'warn',
112+
'no-mixed-requires': 'error',
113+
'no-new-require': 'error',
114+
'no-path-concat': 'error',
115+
116+
117+
118+
'array-bracket-newline': ['error', 'consistent'],
119+
'array-bracket-spacing': ['error', 'never'],
120+
'array-element-newline': ['error', 'consistent'],
121+
'block-spacing': 'error',
122+
'brace-style': 'error',
123+
'camelcase': ['error', { properties: 'never' }],
124+
'comma-dangle': ['error', 'never'],
125+
'comma-spacing': ['error', { before: false, after: true }],
126+
'comma-style': ['error', 'last'],
127+
'computed-property-spacing': ['error', 'never'],
128+
'func-call-spacing': ['error', 'never'],
129+
'indent': ['error', 2, {
130+
VariableDeclarator: 'first',
131+
SwitchCase: 1,
132+
MemberExpression: 1
133+
}],
134+
'jsx-quotes': ['error', 'prefer-double'],
135+
'key-spacing': ['error', { beforeColon: false, afterColon: true, mode: 'strict' }],
136+
'keyword-spacing': 'error',
137+
'line-comment-position': ['error', { position: 'above' }],
138+
'new-parens': 'error',
139+
'no-array-constructor': 'error',
140+
'no-lonely-if': 'error',
141+
'no-mixed-operators': 'error',
142+
'no-mixed-spaces-and-tabs': 'error',
143+
'no-multi-assign': 'error',
144+
'no-multiple-empty-lines': ['error', { max: 1, maxBOF: 0, maxEOF: 1 }],
145+
'no-new-object': 'error',
146+
'no-tabs': 'error',
147+
'no-trailing-spaces': 'error',
148+
'no-unneeded-ternary': 'error',
149+
'no-whitespace-before-property': 'error',
150+
'nonblock-statement-body-position': ['error', 'beside'],
151+
'object-curly-newline': ['error', { consistent: true }],
152+
'object-curly-spacing': ['error', 'always', { objectsInObjects: false }],
153+
'operator-assignment': ['error', 'always'],
154+
'operator-linebreak': ['error', 'before', { overrides: { '||': 'after', '&&': 'after' }}],
155+
'padded-blocks': ['error', 'never'],
156+
'quote-props': ['error', 'as-needed'],
157+
'quotes': ['error', 'single', { allowTemplateLiterals: true }],
158+
'semi': ['error', 'always'],
159+
'semi-spacing': ['error', { before: false, after: true }],
160+
'semi-style': ['error', 'last'],
161+
'space-before-blocks': 'error',
162+
'space-before-function-paren': ['error', { anonymous: 'never', named: 'never', asyncArrow: 'always' }],
163+
'space-in-parens': 'error',
164+
'space-infix-ops': 'error',
165+
'space-unary-ops': ['error', { words: true, nonwords: false }],
166+
'spaced-comment': ['error', 'always', {
167+
line: {
168+
exceptions: ['-', '+']
169+
},
170+
block: {
171+
exceptions: ['*'],
172+
balanced: true
173+
}
174+
}],
175+
'switch-colon-spacing': 'error',
176+
'template-tag-spacing': 'error',
177+
178+
179+
180+
'arrow-parens': ['error', 'as-needed', { requireForBlockBody: true }],
181+
'arrow-spacing': 'error',
182+
'constructor-super': 'error',
183+
'generator-star-spacing': ['error', { before: true, after: false }],
184+
'no-class-assign': 'error',
185+
'no-confusing-arrow': ['error', { allowParens: true }],
186+
'no-const-assign': 'error',
187+
'no-dupe-class-members': 'error',
188+
'no-duplicate-imports': 'error',
189+
'no-new-symbol': 'error',
190+
'no-this-before-super': 'error',
191+
'no-useless-computed-key': 'error',
192+
'no-useless-constructor': 'error',
193+
'no-useless-rename': 'error',
194+
'require-yield': 'error',
195+
'rest-spread-spacing': 'error',
196+
'template-curly-spacing': 'error',
197+
'yield-star-spacing': ['error', 'before']
198+
}
199+
}

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules/
2+
3+
*.code-workspace
4+
5+
package-lock.json

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2019 Adam Davies
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<a href="https://travis-ci.org/adamdavies001/react-hooks-object-state"><img src="https://travis-ci.org/adamdavies001/react-hooks-object-state.svg?branch=master" alt="Travis Build Status"></a>
2+
<a href="https://ci.appveyor.com/project/adamdavies001/react-hooks-object-state"><img src="https://ci.appveyor.com/api/projects/status/jc0a2g2t7t4logcu/branch/master?svg=true" alt="Windows Build Status"></a>
3+
<a href="https://badge.fury.io/js/react-hooks-object-state"><img src="https://badge.fury.io/js/react-hooks-object-state.svg" alt="npm version"></a>
4+
5+
## About
6+
7+
This package is a React hook for partially updating object states within functional components that avoids the default behavior of `useState` that overwrites the entire object state. It reflects the merge behavior of `setState` used in classical components.
8+
9+
**Use this** when you need an object state that shouldn't be split up into multiple states.
10+
**Don't use this** if you only need an object state with a few simple properties.
11+
12+
## Features
13+
14+
- Partially update object values in state without erasing any non-updated entries
15+
- Calculate new values based on the previous state with a function argument
16+
17+
## Install
18+
19+
```bash
20+
$ npm install react-hooks-object-state
21+
```
22+
23+
Peer dependencies: react (^16.8.0)
24+
25+
## Usage
26+
27+
Within a functional component, simply declare and use the `useObjectState` hook to create a state object. Then pass any object updates to the returned setter function to update the original object.
28+
29+
```jsx
30+
import React from 'react'
31+
import useObjectState from 'react-hooks-object-state'
32+
33+
const Example = () => {
34+
const [myObject, setMyObject] = useObjectState({bool: true, string: 'foo'})
35+
36+
const updateObject = () => {
37+
setMyObject({bool: false})
38+
}
39+
40+
return <button onClick={updateObject}>Update object</button>
41+
}
42+
43+
// myObject after update:
44+
// {
45+
// bool: false,
46+
// string: 'foo'
47+
// }
48+
```
49+
50+
#### Passing a function
51+
52+
Alternatively, you can pass a function to the setter if you need to use the previous state to calculate new state values.
53+
54+
```jsx
55+
const updateObject = () => {
56+
setMyObject((state) => {
57+
return {
58+
string: state.str + 'bar'
59+
}
60+
})
61+
}
62+
```
63+
64+
The use of `props` in function arguments is not included since hooks are not able to read component props, and workarounds would not effectively replicate the classical `setState` behavior.
65+
66+
#### Additional info
67+
68+
An initial object **must** be provided to `useObjectState`. This hook deep-merges objects by copying common entries from a source to a target object.
69+
70+
Like the classical `setState` method, this does not create entries if they don't already exist. Providing an empty initial object will always result in an empty object.

index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
module.exports = {
4+
environments: require('./lib/environments'),
5+
rules: {
6+
'script-type': require('./lib/rules/script-type'),
7+
'api-version': require('./lib/rules/api-version'),
8+
'no-invalid-modules': require('./lib/rules/no-invalid-modules'),
9+
'no-extra-modules': require('./lib/rules/no-extra-modules'),
10+
'no-log-module': require('./lib/rules/no-log-module'),
11+
'log-args': require('./lib/rules/log-args'),
12+
'module-vars': require('./lib/rules/module-vars'),
13+
'no-amd-name': require('./lib/rules/no-amd-name'),
14+
'entry-points': require('./lib/rules/entry-points')
15+
}
16+
};

0 commit comments

Comments
 (0)