Skip to content

Commit 291b676

Browse files
docs(escape-hatches): mark lint-intentional example + suppress in sandbox (#8045)
1 parent 79c15bc commit 291b676

File tree

1 file changed

+49
-12
lines changed

1 file changed

+49
-12
lines changed

src/content/learn/escape-hatches.md

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,65 @@ const ref = useRef(0);
3131

3232
Like state, refs are retained by React between re-renders. However, setting state re-renders a component. Changing a ref does not! You can access the current value of that ref through the `ref.current` property.
3333

34+
For example, this Effect depends on the `options` object which gets re-created every time you edit the input:
35+
36+
> ⚠️ **Note (example intentionally shows a problematic pattern).**
37+
>
38+
> The code below purposely shows a case where an `options` object is recreated on every render to demonstrate why that would re-run an Effect. The sandbox linter will show an error for this example — that is expected. See the following section for a corrected version.
39+
3440
<Sandpack>
3541

3642
```js
37-
import { useRef } from 'react';
43+
import { useState, useEffect } from 'react';
44+
import { createConnection } from './chat.js';
3845

39-
export default function Counter() {
40-
let ref = useRef(0);
46+
const serverUrl = 'https://localhost:1234';
4147

42-
function handleClick() {
43-
ref.current = ref.current + 1;
44-
alert('You clicked ' + ref.current + ' times!');
45-
}
48+
function ChatRoom({ roomId }) {
49+
const [message, setMessage] = useState('');
50+
51+
const options = {
52+
serverUrl: serverUrl,
53+
roomId: roomId
54+
};
55+
56+
// eslint-disable-next-line react-hooks/exhaustive-deps
57+
useEffect(() => {
58+
const connection = createConnection(options);
59+
connection.connect();
60+
return () => connection.disconnect();
61+
}, [options]);
62+
63+
return (
64+
<>
65+
<h1>Welcome to the {roomId} room!</h1>
66+
<input value={message} onChange={e => setMessage(e.target.value)} />
67+
</>
68+
);
69+
}
70+
</Sandpack>
4671

72+
export default function App() {
73+
const [roomId, setRoomId] = useState('general');
4774
return (
48-
<button onClick={handleClick}>
49-
Click me!
50-
</button>
75+
<>
76+
<label>
77+
Choose the chat room:{' '}
78+
<select
79+
value={roomId}
80+
onChange={e => setRoomId(e.target.value)}
81+
>
82+
<option value="general">general</option>
83+
<option value="travel">travel</option>
84+
<option value="music">music</option>
85+
</select>
86+
</label>
87+
<hr />
88+
<ChatRoom roomId={roomId} />
89+
</>
5190
);
5291
}
53-
```
5492

55-
</Sandpack>
5693

5794
A ref is like a secret pocket of your component that React doesn't track. For example, you can use refs to store [timeout IDs](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#return_value), [DOM elements](https://developer.mozilla.org/en-US/docs/Web/API/Element), and other objects that don't impact the component's rendering output.
5895

0 commit comments

Comments
 (0)