Skip to content

Commit be01fb7

Browse files
committed
fix(types): typings
1 parent e4da050 commit be01fb7

12 files changed

+30
-183
lines changed

.nyc_output/f87f99cb-1220-4a09-b453-b6f80a360070.json

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"uuid":"93918fce-dec8-48ea-a76b-0bbcaf6021f0","parent":null,"pid":67620,"argv":["/Users/rwieruch/.nvm/versions/node/v16.14.0/bin/node","/Users/rwieruch/Developer/Side/use-state-with-callback/node_modules/.bin/mocha","--require","@babel/register","--require","./test/dom.js","src/**/**spec.js"],"execArgv":[],"cwd":"/Users/rwieruch/Developer/Side/use-state-with-callback","time":1649947332870,"ppid":67615,"root":"55cc90f8-2611-4c8c-b8be-2cddc80edac2","coverageFilename":"/Users/rwieruch/Developer/Side/use-state-with-callback/.nyc_output/93918fce-dec8-48ea-a76b-0bbcaf6021f0.json","files":[]}

.nyc_output/processinfo/9a889e5a-e32a-4e37-9057-00df28a0f0af.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

.nyc_output/processinfo/f87f99cb-1220-4a09-b453-b6f80a360070.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

.nyc_output/processinfo/index.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"processes":{"9a889e5a-e32a-4e37-9057-00df28a0f0af":{"parent":null,"children":["f87f99cb-1220-4a09-b453-b6f80a360070"]},"f87f99cb-1220-4a09-b453-b6f80a360070":{"parent":"9a889e5a-e32a-4e37-9057-00df28a0f0af","children":[]}},"files":{"/Users/mydspr/Developer/Repos/use-state-with-callback/src/spec.js":["f87f99cb-1220-4a09-b453-b6f80a360070"],"/Users/mydspr/Developer/Repos/use-state-with-callback/src/index.js":["f87f99cb-1220-4a09-b453-b6f80a360070"]},"externalIds":{}}
1+
{"processes":{"93918fce-dec8-48ea-a76b-0bbcaf6021f0":{"parent":null,"children":[]}},"files":{},"externalIds":{}}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# useStateWithCallback React Hook
22

3-
[![Build Status](https://travis-ci.org/the-road-to-learn-react/use-state-with-callback.svg?branch=master)](https://travis-ci.org/the-road-to-learn-react/use-state-with-callback) [![Slack](https://slack-the-road-to-learn-react.wieruch.com/badge.svg)](https://slack-the-road-to-learn-react.wieruch.com/) [![Greenkeeper badge](https://badges.greenkeeper.io/the-road-to-learn-react/use-state-with-callback.svg)](https://greenkeeper.io/) [![Coverage Status](https://coveralls.io/repos/github/the-road-to-learn-react/use-state-with-callback/badge.svg?branch=master)](https://coveralls.io/github/the-road-to-learn-react/use-state-with-callback?branch=master) ![NPM](https://img.shields.io/npm/l/use-state-with-callback.svg)
3+
[![Build Status](https://travis-ci.org/the-road-to-learn-react/use-state-with-callback.svg?branch=master)](https://travis-ci.org/the-road-to-learn-react/use-state-with-callback) [![Slack](https://slack-the-road-to-learn-react.wieruch.com/badge.svg)](https://slack-the-road-to-learn-react.wieruch.com/) [![Greenkeeper badge](https://badges.greenkeeper.io/the-road-to-learn-react/use-state-with-callback.svg)](https://greenkeeper.io/) ![NPM](https://img.shields.io/npm/l/use-state-with-callback.svg)
44

55
Custom hook to include a callback function for useState which was previously available for setState in class components. [Read more about it here.](https://www.robinwieruch.de/react-usestate-callback/)
66

package.json

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
"main": "lib/index.js",
66
"scripts": {
77
"build": "webpack --config ./webpack.config.js --mode=production",
8-
"prepare": "npm run build",
9-
"test": "nyc mocha --require @babel/register --require ./test/dom.js 'src/**/**spec.js'",
10-
"coverage": "nyc report --reporter=text-lcov | coveralls"
8+
"prepare": "npm run build"
119
},
1210
"keywords": [],
1311
"author": "“Robin <[email protected]”> (https://www.robinwieruch.de)",
@@ -25,11 +23,7 @@
2523
"@babel/register": "^7.4.4",
2624
"babel-eslint": "^10.0.2",
2725
"babel-loader": "^8.0.5",
28-
"chai": "^4.2.0",
2926
"copy-webpack-plugin": "^5.1.1",
30-
"coveralls": "^3.0.3",
31-
"enzyme": "^3.9.0",
32-
"enzyme-adapter-react-16": "^1.13.2",
3327
"eslint": "^5.16.0",
3428
"eslint-config-airbnb": "^18.0.0",
3529
"eslint-config-prettier": "^6.0.0",
@@ -39,13 +33,9 @@
3933
"eslint-plugin-prettier": "^3.1.0",
4034
"eslint-plugin-react": "^7.14.2",
4135
"eslint-plugin-react-hooks": "^1.6.1",
42-
"jsdom": "^15.1.1",
43-
"mocha": "^6.1.4",
44-
"nyc": "^14.1.1",
4536
"prettier": "^1.18.2",
4637
"react": "^16.8.6",
4738
"react-dom": "^16.8.6",
48-
"sinon": "^7.3.2",
4939
"webpack": "^4.31.0",
5040
"webpack-cli": "^3.3.2"
5141
},

src/index.d.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1-
import React, {SetStateAction, Dispatch} from 'react';
1+
import { SetStateAction, Dispatch } from 'react';
22

3-
type DispatchWithCallback<A> = (value: A, callback: Callback) => void;
3+
type DispatchWithCallback<A, S> = (
4+
value: A,
5+
callback: Callback<S>,
6+
) => void;
47

5-
type Callback<S> = (state:s) => (void | (() => void | undefined));
8+
type Callback<S> = (state: S) => void | (() => void | undefined);
69

10+
declare function useStateWithCallback<S>(
11+
initialState: S,
12+
callback: Callback<S>,
13+
): [S, Dispatch<SetStateAction<S>>];
714

8-
declare function useStateWithCallback<S>(initialState:S,callback:Callback) : [S,Dispatch<SetStateAction<S>>];
15+
export declare function useStateWithCallbackInstant<S>(
16+
initialState: S,
17+
callback: Callback<S>,
18+
): [S, Dispatch<SetStateAction<S>>];
919

10-
export declare function useStateWithCallbackInstant<S>(initialState:S,callback:Callback) : [S,Dispatch<SetStateAction<S>>];
20+
export declare function useStateWithCallbackLazy<S>(
21+
initialState: S,
22+
): [S, DispatchWithCallback<SetStateAction<S>, S>];
1123

12-
13-
export declare function useStateWithCallbackLazy<S>(initialState:S) : [S,DispatchWithCallback<SetStateAction<S>>];
14-
15-
16-
export default useStateWithCallback;
24+
export default useStateWithCallback;

src/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
import { useState, useEffect, useLayoutEffect, useRef } from 'react';
1+
import * as React from 'react';
22

33
const useStateWithCallback = (initialState, callback) => {
4-
const [state, setState] = useState(initialState);
4+
const [state, setState] = React.useState(initialState);
55

6-
useEffect(() => callback(state), [state, callback]);
6+
React.useEffect(() => callback(state), [state, callback]);
77

88
return [state, setState];
99
};
1010

1111
const useStateWithCallbackInstant = (initialState, callback) => {
12-
const [state, setState] = useState(initialState);
12+
const [state, setState] = React.useState(initialState);
1313

14-
useLayoutEffect(() => callback(state), [state, callback]);
14+
React.useLayoutEffect(() => callback(state), [state, callback]);
1515

1616
return [state, setState];
1717
};
1818

1919
const useStateWithCallbackLazy = initialValue => {
20-
const callbackRef = useRef(null);
20+
const callbackRef = React.useRef(null);
2121

22-
const [value, setValue] = useState(initialValue);
22+
const [value, setValue] = React.useState(initialValue);
2323

24-
useEffect(() => {
24+
React.useEffect(() => {
2525
if (callbackRef.current) {
2626
callbackRef.current(value);
2727

0 commit comments

Comments
 (0)