Skip to content

Commit 4a66fc1

Browse files
author
Agney
committed
error for valid react element
1 parent 706749a commit 4a66fc1

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

example/src/IconList.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ const spinners = [
3535
const IconList = () => {
3636
return (
3737
<ul>
38-
{spinners.map(({ indicator: Indicator, width }) => (
39-
<SpinnerSection indicator={<Indicator width={width} />} />
38+
{spinners.map(({ indicator: Indicator, width }, index) => (
39+
<SpinnerSection indicator={<Indicator width={width} key={index} />} />
4040
))}
4141
</ul>
4242
);

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,8 @@
7373
},
7474
"lint-staged": {
7575
"*.{js,css,md}": "prettier --write"
76+
},
77+
"dependencies": {
78+
"tiny-invariant": "^1.1.0"
7679
}
7780
}

src/useLoading.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { cloneElement, ReactElement } from 'react';
1+
import { cloneElement, ReactElement, isValidElement } from 'react';
2+
import invariant from 'tiny-invariant';
23

34
import { useLoaderContext } from './LoaderContext';
45

@@ -17,12 +18,16 @@ type AriaLive = 'off' | 'assertive' | 'polite' | undefined;
1718
* Hook returning Indicator element according to loading argument.
1819
* @example const { containerProps, indicatorEl } = useLoading({ loading: true })
1920
*/
20-
export function useLoading({ loading = false, indicator, loaderProps }: Props) {
21+
export function useLoading({ loading = false, indicator, loaderProps = {} }: Props) {
2122
const containerProps = {
2223
'aria-busy': loading,
2324
'aria-live': 'polite' as AriaLive,
2425
};
2526

27+
const loaderContext = useLoaderContext();
28+
const indicatorEl = indicator ?? loaderContext?.indicator;
29+
invariant(isValidElement(indicatorEl), 'Expected a valid React element as indicator');
30+
2631
const accessibleLoaderProps = (() => {
2732
const { valueText, ...rest } = loaderProps;
2833
return {
@@ -31,9 +36,6 @@ export function useLoading({ loading = false, indicator, loaderProps }: Props) {
3136
...rest,
3237
}
3338
})();
34-
35-
const loaderContext = useLoaderContext();
36-
const indicatorEl = indicator ?? loaderContext?.indicator;
3739
const accessibleIndicator = indicatorEl ? cloneElement(indicatorEl, accessibleLoaderProps) : null;
3840

3941
return {

0 commit comments

Comments
 (0)