Skip to content

Commit

Permalink
docs: added reset state to the ARRAY readme
Browse files Browse the repository at this point in the history
build: added ESLint check to the build step
fix: fixed ESLint issues + added comments to ensure no stupid "fixes" are applied
  • Loading branch information
RIP21 committed Sep 9, 2019
1 parent a6d176b commit d7cefdf
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ yarn-error.log*
/main/
/es6/
/lib/
tsconfig-require.tsbuildinfo
3 changes: 2 additions & 1 deletion README-ARRAY.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,14 @@ Actions:
## useSetState

```jsx
const [state, setState] = useSetState({ loading: false });
const [state, setState, resetState] = useSetState({ loading: false });
setState({ loading: true, data: [1, 2, 3] });
```

Actions:

- `setState(value)` - will merge the `value` with the current `state` (like this.setState works in React)
- `resetState()` - will reset the current `state` to the `value` which you pass to the `useSetState` hook

Properties:

Expand Down
3 changes: 1 addition & 2 deletions example/src/App.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import { useInput, useBoolean, useNumber, useArray, useSetState } from 'react-hanger';
import { useInput, useBoolean, useNumber, useArray } from 'react-hanger';

// eslint-disable-next-line react/prop-types
const Counter = ({ counter }) => {
Expand Down Expand Up @@ -30,7 +30,6 @@ const App = () => {
});
const counter = useNumber(0);
const todos = useArray(['hi there', 'sup', 'world']);
const { state, setState } = useSetState({ loading: false, data: true });

return (
<div style={{ padding: 20 }}>
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-hanger",
"version": "2.1.0",
"version": "2.1.1",
"description": "Set of a helpful hooks, for different specific to some primitives types state changing helpers",
"author": "kitze",
"license": "MIT",
Expand All @@ -26,7 +26,7 @@
"build:es6": "tsc -p tsconfig.json",
"build:require": "tsc -p tsconfig-require.json",
"prebuild": "rimraf lib && mkdir lib && npm run test",
"build": "npm run build:es6 && npm run build:require",
"build": "eslint ./src --ext ts,tsx --max-warnings 0 && npm run build:es6 && npm run build:require",
"postbuild": "node copyPackageJsonAndReadme.js",
"prerelease": "npm run build",
"release": "npm publish lib",
Expand Down
4 changes: 4 additions & 0 deletions src/array/useSetState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export function useSetState<T extends object>(initialValue: T): UseSetState<T> {
},
[setValue],
);
// Disabled on purpose to avoid new references on each render.
// Since initialValue will be object and new reference is
// guaranteed here, while values are the same, hence we can keep using old function
// eslint-disable-next-line react-hooks/exhaustive-deps
const resetState = useCallback(() => setValue(initialValue), []);

return [value, setState, resetState];
Expand Down
2 changes: 1 addition & 1 deletion src/useMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { UseStateful } from './useStateful';
import useMapArray, { UseMapActions } from './array/useMap';

export type MapOrEntries<K, V> = Map<K, V> | [K, V][];
export type UseMap<K, V> = UseStateful<Map<K, V>> & UseMapActions<K, V>
export type UseMap<K, V> = UseStateful<Map<K, V>> & UseMapActions<K, V>;

export function useMap<K, V>(initialState: MapOrEntries<K, V> = new Map()): UseMap<K, V> {
const [map, actions] = useMapArray(initialState);
Expand Down

0 comments on commit d7cefdf

Please sign in to comment.