|
2 | 2 |
|
3 | 3 | // This should trigger an error breaking eslint-plugin-react-hooks: |
4 | 4 | // react-hooks/exhaustive-deps |
| 5 | +// react-hooks/rules-of-hooks |
| 6 | +// react-hooks/error-boundaries |
5 | 7 |
|
6 | | -import { useState, useEffect } from "react"; |
| 8 | +import { useEffect, useState } from "react"; |
| 9 | +import { View } from "react-native"; |
7 | 10 |
|
8 | | -const MyComponent = () => { |
9 | | - const [count, setCount] = useState(0); |
| 11 | +// react-hooks/exhaustive-deps |
| 12 | +// Missing `count` in the dependency array |
| 13 | +const ExhaustiveDepsComponent = () => { |
| 14 | + const [count] = useState(0); |
| 15 | + |
| 16 | + useEffect(() => void String(count), []); |
| 17 | + |
| 18 | + return <View />; |
| 19 | +}; |
10 | 20 |
|
11 | | - useEffect(() => { |
12 | | - setCount(count + 1); |
13 | | - }, []); |
| 21 | +// react-hooks/rules-of-hooks |
| 22 | +// Hooks must not be called conditionally |
| 23 | +const RulesOfHooksComponent = ({ isEnabled }: { isEnabled: boolean }) => { |
| 24 | + if (isEnabled) { |
| 25 | + const [value] = useState("conditional"); |
| 26 | + return <View testID={value} />; |
| 27 | + } |
14 | 28 |
|
15 | | - return <></>; |
| 29 | + return <View />; |
16 | 30 | }; |
17 | 31 |
|
18 | | -export default MyComponent; |
| 32 | +// react-hooks/error-boundaries |
| 33 | +// Use error boundaries instead of try/catch for child component errors |
| 34 | +const ErrorBoundaryComponent = () => { |
| 35 | + try { |
| 36 | + const result = JSON.parse("{}") as Record<string, string>; |
| 37 | + return <View testID={result["id"]} />; |
| 38 | + } catch { |
| 39 | + return <View testID="fallback" />; |
| 40 | + } |
| 41 | +}; |
| 42 | + |
| 43 | +export { |
| 44 | + ErrorBoundaryComponent, |
| 45 | + ExhaustiveDepsComponent, |
| 46 | + RulesOfHooksComponent, |
| 47 | +}; |
0 commit comments