Skip to content

Commit 24cfaab

Browse files
WoLewickiHarvey Connor
andauthored
Add onLoad method to SvgUri (#1817)
Co-authored-by: Harvey Connor <[email protected]>
1 parent c87f823 commit 24cfaab

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

USAGE.md

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,31 @@ If remote SVG file contains CSS in `<style>` element, use `SvgCssUri`:
9191

9292
```jsx
9393
import * as React from 'react';
94-
import { SvgCssUri } from 'react-native-svg';
95-
96-
export default () => (
97-
<SvgCssUri
98-
width="100%"
99-
height="100%"
100-
uri="http://thenewcode.com/assets/svg/accessibility.svg"
101-
/>
102-
);
94+
import { ActivityIndicator, View, StyleSheet } from 'react-native';
95+
import { SvgUri } from 'react-native-svg';
96+
export default function TestComponent() {
97+
const [loading, setLoading] = React.useState(true);
98+
const onError = (e: Error) => {
99+
console.log(e.message);
100+
setLoading(false);
101+
};
102+
const onLoad = () => {
103+
console.log('Svg loaded!');
104+
setLoading(false);
105+
};
106+
return (
107+
<>
108+
<SvgUri
109+
width="100"
110+
height="100"
111+
uri="https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/ruby.svg"
112+
onError={onError}
113+
onLoad={onLoad}
114+
/>
115+
{loading && <ActivityIndicator size="large" color="#0000ff"/>}
116+
</>
117+
);
118+
}
103119
```
104120

105121
## Error handling

src/index.js.flow

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,8 @@ export type UriProps = {
492492
uri: string | null,
493493
onError?: (error: Error) => void,
494494
override?: SvgProps,
495+
onError?: (error: Error) => void,
496+
onLoad?: () => void,
495497
...
496498
} & SvgProps;
497499
export type UriState = {

src/xml.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export interface JsxAST extends AST {
8484
export type AdditionalProps = {
8585
onError?: (error: Error) => void;
8686
override?: Object;
87+
onLoad?: () => void;
8788
};
8889

8990
export type UriProps = SvgProps & { uri: string | null } & AdditionalProps;
@@ -132,11 +133,18 @@ export async function fetchText(uri: string) {
132133
}
133134

134135
export function SvgUri(props: UriProps) {
135-
const { onError = err, uri } = props;
136+
const { onError = err, uri, onLoad } = props;
136137
const [xml, setXml] = useState<string | null>(null);
137138
useEffect(() => {
138-
uri ? fetchText(uri).then(setXml).catch(onError) : setXml(null);
139-
}, [onError, uri]);
139+
uri
140+
? fetchText(uri)
141+
.then((data) => {
142+
setXml(data);
143+
onLoad?.();
144+
})
145+
.catch(onError)
146+
: setXml(null);
147+
}, [onError, uri, onLoad]);
140148
return <SvgXml xml={xml} override={props} />;
141149
}
142150

0 commit comments

Comments
 (0)