Skip to content
This repository was archived by the owner on Apr 3, 2024. It is now read-only.

Commit b1c53b2

Browse files
committed
Add changeset
1 parent 098a43f commit b1c53b2

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

.changeset/three-chefs-play.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
'use-effect-reducer': minor
3+
---
4+
5+
Added support for initial effects, via the 2nd argument to `useEffectReducer`, which can either be a static `initialState` or a function that takes in `exec` and returns an initial state as well as executing initial effects:
6+
7+
```js
8+
const fetchReducer = (state, event) => {
9+
if (event.type === 'RESOLVE') {
10+
return {
11+
...state,
12+
data: event.data,
13+
};
14+
}
15+
16+
return state;
17+
};
18+
19+
const getInitialState = exec => {
20+
exec({ type: 'fetchData', someQuery: '*' });
21+
22+
return { data: null };
23+
};
24+
25+
// (in the component)
26+
const [state, dispatch] = useEffectReducer(fetchReducer, getInitialState, {
27+
fetchData(_, { someQuery }) {
28+
fetch(`/some/api?${someQuery}`)
29+
.then(res => res.json())
30+
.then(data => {
31+
dispatch({
32+
type: 'RESOLVE',
33+
data,
34+
});
35+
});
36+
},
37+
});
38+
```

0 commit comments

Comments
 (0)