Skip to content

Commit 70b609e

Browse files
authored
Merge pull request #760 from reactjs/sync-ee770567
Sync with reactjs.org @ ee77056
2 parents 3e5d2b1 + aa47e76 commit 70b609e

File tree

7 files changed

+53
-84
lines changed

7 files changed

+53
-84
lines changed

beta/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# reactjs.org
22

3-
This repo contains the source code and documentation powering [reactjs.org](https://reactjs.org/).
3+
This repo contains the source code and documentation powering [beta.reactjs.org](https://beta.reactjs.org/).
44

55

66
## Getting started

beta/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"check-all": "npm-run-all prettier lint:fix tsc"
2323
},
2424
"dependencies": {
25-
"@codesandbox/sandpack-react": "v0.19.8-experimental.4",
25+
"@codesandbox/sandpack-react": "v0.19.8-experimental.7",
2626
"@docsearch/css": "3.0.0-alpha.41",
2727
"@docsearch/react": "3.0.0-alpha.41",
2828
"@headlessui/react": "^1.3.0",

beta/src/components/MDX/Sandpack/Console.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ export const SandpackConsole = () => {
106106
<div
107107
key={id}
108108
className={cn(
109-
'last:border-none border-b dark:border-gray-700 text-md p-1 pl-2 leading-6 font-mono',
109+
'last:border-none border-b dark:border-gray-700 text-md p-1 pl-2 leading-6 font-mono min-h-[32px]',
110110
`console-${getType(method)}`
111111
)}>
112-
<span className={cn('console-message')}>
112+
<span className="console-message">
113113
{data.map((msg, index) => {
114114
if (typeof msg === 'string') {
115115
return <span key={`${msg}-${index}`}>{msg}</span>;

beta/src/pages/learn/you-might-not-need-an-effect.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,11 @@ function List({ items }) {
232232
}
233233
```
234234

235-
**This pattern can be hard to understand, but it’s better than updating state in an Effect.** In the above example, `setSelection` is called directly during a render. React will re-render the `List` *immediately* after it exits with a `return` statement. By that point, React hasn't rendered the `List` children or updated the DOM yet, so this lets the `List` children skip rendering the stale `selection` value. [Read more about using this pattern correctly.](/apis/usestate#storing-information-from-previous-renders)
235+
[Storing information from previous renders](/apis/usestate#storing-information-from-previous-renders) like this can be hard to understand, but it’s better than updating the same state in an Effect. In the above example, `setSelection` is called directly during a render. React will re-render the `List` *immediately* after it exits with a `return` statement. By that point, React hasn't rendered the `List` children or updated the DOM yet, so this lets the `List` children skip rendering the stale `selection` value.
236236

237-
Before moving on, consider whether you can further simplify the requirements to calculate everything during rendering. For example, instead of storing (and resetting) the selected *item*, you can store the selected *item ID:*
237+
When you update a component during rendering, React throws away the returned JSX and immediately retries rendering. To avoid very slow cascading retries, React only lets you update the *same* component's state during a render. If you update another component's state during a render, you'll see an error. A condition like `items !== prevItems` is necessary to avoid loops. You may adjust state like this, but any other side effects (like changing the DOM or setting a timeout) should remain in event handlers or Effects to [keep your components predictable](/learn/keeping-components-pure).
238+
239+
**Although this pattern is more efficient than an Effect, most components shouldn't need it either.** No matter how you do it, adjusting state based on props or other state makes your data flow more difficult to understand and debug. Always check whether you can [reset all state with a key](#resetting-all-state-when-a-prop-changes) or [calculate everything during rendering](#updating-state-based-on-props-or-state) instead. For example, instead of storing (and resetting) the selected *item*, you can store the selected *item ID:*
238240

239241
```js {3-5}
240242
function List({ items }) {

beta/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -555,10 +555,10 @@
555555
codesandbox-import-utils "^1.2.3"
556556
lodash.isequal "^4.5.0"
557557

558-
"@codesandbox/[email protected].4":
559-
version "0.19.8-experimental.4"
560-
resolved "https://registry.yarnpkg.com/@codesandbox/sandpack-react/-/sandpack-react-0.19.8-experimental.4.tgz#fb170bcca42b0e68175edd1510973b779504d416"
561-
integrity sha512-SyfKEb6iPT4ZqQJ3wzf96OgDj1WmfvZs9tt0HHzA8InwGCPm9QWcJcQbKatEvu+S7sOBt2rxcETelLYhddn9nA==
558+
"@codesandbox/[email protected].7":
559+
version "0.19.8-experimental.7"
560+
resolved "https://registry.yarnpkg.com/@codesandbox/sandpack-react/-/sandpack-react-0.19.8-experimental.7.tgz#44724fc0cce5d470eae9d6bc757ae237b699c019"
561+
integrity sha512-+zT07s7LYxWq4bQYEStZszpfQAbbqP7PukF0TlJxYbjDVDXDhikdBkxuD1hXvVA6FOPdgLuF2fdnDR5NViktwA==
562562
dependencies:
563563
"@code-hike/classer" "^0.0.0-aa6efee"
564564
"@codemirror/closebrackets" "^0.19.0"

content/community/conferences.md

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,6 @@ Do you know of a local React.js conference? Add it here! (Please keep the list c
1212

1313
## Upcoming Conferences {#upcoming-conferences}
1414

15-
16-
### render(ATL) 2022 🍑 {#render-atlanta-2022}
17-
June 1 - 4, 2022. Atlanta, GA, USA
18-
19-
[Website](https://renderatl.com) - [Discord](https://www.renderatl.com/discord) - [Twitter](https://twitter.com/renderATL) - [Instagram](https://www.instagram.com/renderatl/) - [Facebook](https://www.facebook.com/renderatl/) - [LinkedIn](https://www.linkedin.com/company/renderatl) - [Podcast](https://www.renderatl.com/culture-and-code#/)
20-
21-
### React Day Bangalore 2022 {#reactday-bangalore-2022}
22-
June 8 - 9, 2022. Remote
23-
24-
[Website](https://reactday.in/) - [Twitter](https://twitter.com/ReactDayIn) - [Linkedin](https://www.linkedin.com/company/react-day/) - [YouTube](https://www.youtube.com/reactify_in)
25-
26-
### App.js Conf 2022 {#appjs-conf-2022}
27-
June 8 - 10, 2022. In-person in Kraków, Poland + remote
28-
29-
[Website](https://appjs.co) - [Twitter](https://twitter.com/appjsconf)
30-
31-
### React Summit 2022 {#react-summit-2022}
32-
June 17 & 21, 2022. In-person in Amsterdam, Netherlands + remote first interactivity (hybrid event)
33-
34-
[Website](https://reactsummit.com) - [Twitter](https://twitter.com/reactsummit) - [Facebook](https://www.facebook.com/reactamsterdam) - [Videos](https://youtube.com/c/ReactConferences)
35-
36-
### React Norway 2022 {#react-norway-2022}
37-
June 24, 2022. In-person at Farris Bad Hotel in Larvik, Norway and online (hybrid event).
38-
39-
[Website](https://reactnorway.com/) - [Twitter](https://twitter.com/ReactNorway)
40-
41-
### ReactNext 2022 {#react-next-2022}
42-
June 28, 2022. Tel-Aviv, Israel
43-
44-
[Website](https://react-next.com) - [Twitter](https://twitter.com/ReactNext) - [Videos](https://www.youtube.com/c/ReactNext)
45-
4615
### React Native EU 2022: Powered by {callstack} {#react-native-eu-2022-powered-by-callstack}
4716
September 1-2, 2022 - Remote event
4817

@@ -74,6 +43,36 @@ May, 2023. Salt Lake City, UT
7443

7544
## Past Conferences {#past-conferences}
7645

46+
### ReactNext 2022 {#react-next-2022}
47+
June 28, 2022. Tel-Aviv, Israel
48+
49+
[Website](https://react-next.com) - [Twitter](https://twitter.com/ReactNext) - [Videos](https://www.youtube.com/c/ReactNext)
50+
51+
### React Norway 2022 {#react-norway-2022}
52+
June 24, 2022. In-person at Farris Bad Hotel in Larvik, Norway and online (hybrid event).
53+
54+
[Website](https://reactnorway.com/) - [Twitter](https://twitter.com/ReactNorway)
55+
56+
### React Summit 2022 {#react-summit-2022}
57+
June 17 & 21, 2022. In-person in Amsterdam, Netherlands + remote first interactivity (hybrid event)
58+
59+
[Website](https://reactsummit.com) - [Twitter](https://twitter.com/reactsummit) - [Facebook](https://www.facebook.com/reactamsterdam) - [Videos](https://youtube.com/c/ReactConferences)
60+
61+
### App.js Conf 2022 {#appjs-conf-2022}
62+
June 8 - 10, 2022. In-person in Kraków, Poland + remote
63+
64+
[Website](https://appjs.co) - [Twitter](https://twitter.com/appjsconf)
65+
66+
### React Day Bangalore 2022 {#reactday-bangalore-2022}
67+
June 8 - 9, 2022. Remote
68+
69+
[Website](https://reactday.in/) - [Twitter](https://twitter.com/ReactDayIn) - [Linkedin](https://www.linkedin.com/company/react-day/) - [YouTube](https://www.youtube.com/reactify_in)
70+
71+
### render(ATL) 2022 🍑 {#render-atlanta-2022}
72+
June 1 - 4, 2022. Atlanta, GA, USA
73+
74+
[Website](https://renderatl.com) - [Discord](https://www.renderatl.com/discord) - [Twitter](https://twitter.com/renderATL) - [Instagram](https://www.instagram.com/renderatl/) - [Facebook](https://www.facebook.com/renderatl/) - [LinkedIn](https://www.linkedin.com/company/renderatl) - [Podcast](https://www.renderatl.com/culture-and-code#/)
75+
7776
### RemixConf 2022 {#remixconf-2022}
7877
May 24 - 25, 2022. Salt Lake City, UT
7978

content/docs/hooks-faq.md

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---
1+
---
22
id: hooks-faq
33
title: "Хуки: ответы на вопросы"
44
permalink: docs/hooks-faq.html
@@ -333,54 +333,22 @@ function useWindowPosition() {
333333

334334
### Как получить предыдущие пропсы или состояние? {#how-to-get-the-previous-props-or-state}
335335

336-
Сейчас, вы можете сделать это вручную, [используя реф](#is-there-something-like-instance-variables):
336+
There are two cases in which you might want to get previous props or state.
337337

338-
```js{6,8}
339-
function Counter() {
340-
const [count, setCount] = useState(0);
338+
Sometimes, you need previous props to **clean up an effect.** For example, you might have an effect that subscribes to a socket based on the `userId` prop. If the `userId` prop changes, you want to unsubscribe from the _previous_ `userId` and subscribe to the _next_ one. You don't need to do anything special for this to work:
341339

342-
const prevCountRef = useRef();
343-
useEffect(() => {
344-
prevCountRef.current = count;
345-
});
346-
const prevCount = prevCountRef.current;
347-
348-
return <h1>Сейчас: {count}, до этого: {prevCount}</h1>;
349-
}
350-
```
351-
352-
Это может показаться усложнённым, но вы можете вынести эту логику в отдельный хук:
353-
354-
```js{3,7}
355-
function Counter() {
356-
const [count, setCount] = useState(0);
357-
const prevCount = usePrevious(count);
358-
return <h1>Сейчас: {count}, до этого: {prevCount}</h1>;
359-
}
360-
361-
function usePrevious(value) {
362-
const ref = useRef();
363-
useEffect(() => {
364-
ref.current = value;
365-
});
366-
return ref.current;
367-
}
340+
```js
341+
useEffect(() => {
342+
ChatAPI.subscribeToSocket(props.userId);
343+
return () => ChatAPI.unsubscribeFromSocket(props.userId);
344+
}, [props.userId]);
368345
```
369346

370-
Обратите внимание, как это будет работать для пропсов, состояния или любого другого вычисляемого значения.
371-
372-
```js{5}
373-
function Counter() {
374-
const [count, setCount] = useState(0);
375-
376-
const calculation = count + 100;
377-
const prevCalculation = usePrevious(calculation);
378-
// ...
379-
```
347+
In the above example, if `userId` changes from `3` to `4`, `ChatAPI.unsubscribeFromSocket(3)` will run first, and then `ChatAPI.subscribeToSocket(4)` will run. There is no need to get "previous" `userId` because the cleanup function will capture it in a closure.
380348

381-
Возможно, в будущем API React введёт хук `usePrevious`, так как он требуется довольно часто.
349+
Other times, you might need to **adjust state based on a change in props or other state**. This is rarely needed and is usually a sign you have some duplicate or redundant state. However, in the rare case that you need this pattern, you can [store previous state or props in state and update them during rendering](#how-do-i-implement-getderivedstatefromprops).
382350

383-
Также смотрите [рекомендованный паттерн для производного состояния](#how-do-i-implement-getderivedstatefromprops).
351+
We have previously suggested a custom Hook called `usePrevious` to hold the previous value. However, we've found that most use cases fall into the two patterns described above. If your use case is different, you can [hold a value in a ref](#is-there-something-like-instance-variables) and manually update it when needed. Avoid reading and updating refs during rendering because this makes your component's behavior difficult to predict and understand.
384352

385353
### Почему я вижу устаревшие пропсы или состояние в моей функции? {#why-am-i-seeing-stale-props-or-state-inside-my-function}
386354

0 commit comments

Comments
 (0)