Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update React example to suggest useEffect listener and lifecycle #245

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions versioned_docs/version-v5/main/guides/deep-links.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,34 @@ import { App, URLOpenListenerEvent } from '@capacitor/app';
Next, define the AppUrlListener component, listening for the `appUrlOpen` event then redirecting when a deep link is found:

```typescript
const AppUrlListener: React.FC<any> = () => {
let history = useHistory();
function AppUrlListener(): null {
const history = useHistory();

useEffect(() => {
App.addListener('appUrlOpen', (event: URLOpenListenerEvent) => {
const onAppUrlOpen = (event: URLOpenListenerEvent): void => {
// Example url: https://beerswift.app/tabs/tab2
// slug = /tabs/tab2
const slug = event.url.split('.app').pop();
const slug = event.url.split('.com').pop();
if (slug) {
history.push(slug);
navigate(slug);
}
// If no match, do nothing - let regular routing
// logic take over
};

App.addListener('appUrlOpen', onAppUrlOpen).catch((err) => {
console.log(err);
});
}, []);

return () => {
App.removeAllListeners().catch((err) => {
console.log(err);
});
};
}, [history]);

return null;
};
}

export default AppUrlListener;
```
Expand Down