Skip to content
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
38 changes: 21 additions & 17 deletions docs/crashlytics/usage/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,16 @@ There are various methods to set attributes for the crash report, in order to pr
```js
import React, { useEffect } from 'react';
import { View, Button } from 'react-native';
import crashlytics from '@react-native-firebase/crashlytics';
import { getCrashlytics, log, setUserId, setAttribute, setAttributes, crash } from '@react-native-firebase/crashlytics';

const crashlytics = getCrashlytics();

async function onSignIn(user) {
crashlytics().log('User signed in.');
log(crashlytics, 'User signed in.');
await Promise.all([
crashlytics().setUserId(user.uid),
crashlytics().setAttribute('credits', String(user.credits)),
crashlytics().setAttributes({
setUserId(crashlytics, user.uid),
setAttribute(crashlytics, 'credits', String(user.credits)),
setAttributes(crashlytics, {
role: 'admin',
followers: '13',
email: user.email,
Expand All @@ -78,7 +80,7 @@ async function onSignIn(user) {

export default function App() {
useEffect(() => {
crashlytics().log('App mounted.');
log(crashlytics, 'App mounted.');
}, []);

return (
Expand All @@ -94,7 +96,7 @@ export default function App() {
})
}
/>
<Button title="Test Crash" onPress={() => crashlytics().crash()} />
<Button title="Test Crash" onPress={() => crash(crashlytics)} />
</View>
);
}
Expand All @@ -108,29 +110,30 @@ Crashlytics using the `recordError` method. This will also provide you with the
```jsx
import React, { useState, useEffect } from 'react';
import { View, Text } from 'react-native';
import crashlytics from '@react-native-firebase/crashlytics';
import { getCrashlytics, log, recordError } from '@react-native-firebase/crashlytics';

const crashlytics = getCrashlytics();
const users = [];

export default function App() {
const [userCounts, setUserCounts] = useState(null);

function updateUserCounts() {
crashlytics().log('Updating user count.');
log(crashlytics, 'Updating user count.');
try {
if (users) {
// An empty array is truthy, but not actually true.
// Therefore the array was never initialized.
setUserCounts(userCounts.push(users.length));
}
} catch (error) {
crashlytics().recordError(error);
recordError(crashlytics, error);
console.log(error);
}
}

useEffect(() => {
crashlytics().log('App mounted.');
log(crashlytics, 'App mounted.');
if (users == true) setUserCounts([]);
updateUserCounts();
}, []);
Expand Down Expand Up @@ -159,21 +162,22 @@ This can be done throughout the app with a simple method call to `setCrashlytics
```jsx
import React, { useState } from 'react';
import { View, Button, Text } from 'react-native';
import crashlytics from '@react-native-firebase/crashlytics';
import { getCrashlytics, setCrashlyticsCollectionEnabled, crash } from '@react-native-firebase/crashlytics';

const crashlytics = getCrashlytics();

export default function App() {
const [enabled, setEnabled] = useState(crashlytics().isCrashlyticsCollectionEnabled);
const [enabled, setEnabled] = useState(crashlytics.isCrashlyticsCollectionEnabled);

async function toggleCrashlytics() {
await crashlytics()
.setCrashlyticsCollectionEnabled(!enabled)
.then(() => setEnabled(crashlytics().isCrashlyticsCollectionEnabled));
await setCrashlyticsCollectionEnabled(crashlytics, !enabled)
.then(() => setEnabled(crashlytics.isCrashlyticsCollectionEnabled));
Comment on lines +173 to +174
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better readability and consistency, you can refactor this to use async/await exclusively, instead of mixing it with .then().

Suggested change
await setCrashlyticsCollectionEnabled(crashlytics, !enabled)
.then(() => setEnabled(crashlytics.isCrashlyticsCollectionEnabled));
await setCrashlyticsCollectionEnabled(crashlytics, !enabled);
setEnabled(crashlytics.isCrashlyticsCollectionEnabled);

}

return (
<View>
<Button title="Toggle Crashlytics" onPress={toggleCrashlytics} />
<Button title="Crash" onPress={() => crashlytics().crash()} />
<Button title="Crash" onPress={() => crash(crashlytics)} />
<Text>Crashlytics is currently {enabled ? 'enabled' : 'disabled'}</Text>
</View>
);
Expand Down
Loading