Skip to content

Commit ee38775

Browse files
committed
Set loading state based on presence of default value
1 parent 44784ad commit ee38775

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

util/useLoadingValue.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ type ResetAction = { type: 'reset'; defaultValue?: any };
2020
type ValueAction = { type: 'value'; value: any };
2121
type ReducerAction = ErrorAction | ResetAction | ValueAction;
2222

23-
const initialState: ReducerState = {
24-
loading: true,
25-
};
26-
2723
const reducer = (state: ReducerState, action: ReducerAction): ReducerState => {
2824
switch (action.type) {
2925
case 'error':
@@ -48,9 +44,9 @@ const reducer = (state: ReducerState, action: ReducerAction): ReducerState => {
4844
}
4945
};
5046

51-
export default <T>(defaultValue?: T): LoadingValue<T> => {
47+
export default <T>(defaultValue?: T | null): LoadingValue<T> => {
5248
const [state, dispatch] = useReducer(reducer, {
53-
...initialState,
49+
loading: defaultValue === undefined || defaultValue === null,
5450
value: defaultValue,
5551
});
5652

0 commit comments

Comments
 (0)