Skip to content

Commit

Permalink
Merge pull request #7 from kmoskwiak/feature/no-initial-state
Browse files Browse the repository at this point in the history
Feature/no initial state
  • Loading branch information
kmoskwiak authored Jun 2, 2020
2 parents d174d95 + 11f0515 commit 3a8fe40
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 37 deletions.
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
# Changelog

## 2.x.x

### 2.0.x

Initial state is no longer passed to hook. It will be always set to `null`.

```js
// before
const [data, error] = useSSE(
{ data: 'initial data' }
() => {
return fetch();
},
[]
);
```

```js
// after
const [data, error] = useSSE(() => {
return fetch();
}, []);
```

## 1.x.x

### 1.2.x
Expand Down
17 changes: 5 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,9 @@ import React from "react";
import { useSSE } from "use-sse";

const MyComponent = () => {
const [data, error] = useSSE(
{
title: "my initial data",
},
() => {
return fetch("https://myapi.example.com").then((res) => res.json());
},
[]
);
const [data, error] = useSSE(() => {
return fetch("https://myapi.example.com").then((res) => res.json());
}, []);

return <div>{data.title}</div>;
};
Expand Down Expand Up @@ -79,17 +73,16 @@ hydrate(

## API

### useSSE hook
### useSSE

```js
const [data, error] = useSSE(initial, effect, dependencies);
const [data, error] = useSSE(effect, dependencies);
```

#### Params

| param | type | required | description | example |
| -------------- | -------------------- | -------- | -------------------------------------------------------- | -------------------------------------------------- |
| `initial` | `any` | true | initial state | `{}` |
| `effect` | `() => Promise<any>` | true | effect function returning promise which resolves to data | `() => fetch('example.com').then(res=>res.json())` |
| `dependencies` | `any[]` | false | list of dependencies like in useEffect | [] |

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "use-sse",
"version": "1.2.0",
"version": "2.0.0-beta.1",
"description": "useSSE - use server-side effect",
"main": "dist/useSSE.js",
"repository": {
Expand Down
4 changes: 1 addition & 3 deletions src/useSSE.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,18 @@ declare global {

/**
*
* @param initial initial value of state
* @param effect runction returning promise
* @param dependencies list of dependencies like in useEffect
*/
export function useSSE<T>(
initial: any,
effect: () => Promise<any>,
dependencies?: DependencyList
): T[] {
const internalContext: IInternalContext = useContext(InternalContext);
let callId = internalContext.current;
internalContext.current++;
const ctx: IDataContext = useContext(DataContext);
const [data, setData] = useState(ctx[callId]?.data || initial);
const [data, setData] = useState(ctx[callId]?.data || null);
const [error, setError] = useState(ctx[callId]?.error || null);

if (!internalContext.resolved) {
Expand Down
36 changes: 16 additions & 20 deletions tests/useSSE.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,23 @@ describe("useSSE", () => {
shouldResolveAfter: number = 0
) => {
const CustomElement: FunctionComponent = () => {
const [data, error] = useSSE(
{},
() => {
return new Promise((resolve, reject) => {
if (shouldReject) {
return reject({
code: 401,
messgage: "Not authorized",
});
}
if (shouldResolveAfter) {
setTimeout(() => {
resolve({ data: "my custom data" });
}, shouldResolveAfter);
} else {
const [data, error] = useSSE(() => {
return new Promise((resolve, reject) => {
if (shouldReject) {
return reject({
code: 401,
messgage: "Not authorized",
});
}
if (shouldResolveAfter) {
setTimeout(() => {
resolve({ data: "my custom data" });
}
});
},
[]
);
}, shouldResolveAfter);
} else {
resolve({ data: "my custom data" });
}
});
}, []);
check(data, error);
return <div></div>;
};
Expand Down

0 comments on commit 3a8fe40

Please sign in to comment.