Skip to content

docs(en): merge reactjs.org/main into zh-hans.reactjs.org/main @ c9d0cbfc #1118

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

Merged
merged 7 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/content/blog/2022/03/29/react-v18.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Typically, for the best user experience, a single user input should result in bo


```js
import {startTransition} from 'react';
import { startTransition } from 'react';

// Urgent: Show what was typed
setInputValue(input);
Expand Down
7 changes: 6 additions & 1 deletion src/content/learn/adding-interactivity.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,14 @@ import { sculptureList } from './data.js';
export default function Gallery() {
const [index, setIndex] = useState(0);
const [showMore, setShowMore] = useState(false);
const hasNext = index < sculptureList.length - 1;

function handleNextClick() {
setIndex(index + 1);
if (hasNext) {
setIndex(index + 1);
} else {
setIndex(0);
}
}

function handleMoreClick() {
Expand Down
84 changes: 42 additions & 42 deletions src/content/learn/extracting-state-logic-into-a-reducer.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ translators:
<Sandpack>

```js App.js
import {useState} from 'react';
import { useState } from 'react';
import AddTask from './AddTask.js';
import TaskList from './TaskList.js';

Expand Down Expand Up @@ -84,7 +84,7 @@ const initialTasks = [
```

```js AddTask.js hidden
import {useState} from 'react';
import { useState } from 'react';

export default function AddTask({onAddTask}) {
const [text, setText] = useState('');
Expand All @@ -108,7 +108,7 @@ export default function AddTask({onAddTask}) {
```

```js TaskList.js hidden
import {useState} from 'react';
import { useState } from 'react';

export default function TaskList({tasks, onChangeTask, onDeleteTask}) {
return (
Expand Down Expand Up @@ -466,7 +466,7 @@ export default function tasksReducer(tasks, action) {
最后,你需要将 `tasksReducer` 导入到组件中。记得先从 React 中导入 `useReducer` Hook:

```js
import {useReducer} from 'react';
import { useReducer } from 'react';
```

接下来,你就可以替换掉之前的 `useState`:
Expand Down Expand Up @@ -498,7 +498,7 @@ const [tasks, dispatch] = useReducer(tasksReducer, initialTasks);
<Sandpack>

```js App.js
import {useReducer} from 'react';
import { useReducer } from 'react';
import AddTask from './AddTask.js';
import TaskList from './TaskList.js';

Expand Down Expand Up @@ -579,7 +579,7 @@ const initialTasks = [
```

```js AddTask.js hidden
import {useState} from 'react';
import { useState } from 'react';

export default function AddTask({onAddTask}) {
const [text, setText] = useState('');
Expand All @@ -603,7 +603,7 @@ export default function AddTask({onAddTask}) {
```

```js TaskList.js hidden
import {useState} from 'react';
import { useState } from 'react';

export default function TaskList({tasks, onChangeTask, onDeleteTask}) {
return (
Expand Down Expand Up @@ -683,7 +683,7 @@ li {
<Sandpack>

```js App.js
import {useReducer} from 'react';
import { useReducer } from 'react';
import AddTask from './AddTask.js';
import TaskList from './TaskList.js';
import tasksReducer from './tasksReducer.js';
Expand Down Expand Up @@ -767,7 +767,7 @@ export default function tasksReducer(tasks, action) {
```

```js AddTask.js hidden
import {useState} from 'react';
import { useState } from 'react';

export default function AddTask({onAddTask}) {
const [text, setText] = useState('');
Expand All @@ -791,7 +791,7 @@ export default function AddTask({onAddTask}) {
```

```js TaskList.js hidden
import {useState} from 'react';
import { useState } from 'react';

export default function TaskList({tasks, onChangeTask, onDeleteTask}) {
return (
Expand Down Expand Up @@ -894,7 +894,7 @@ Reducers 并非没有缺点!以下是比较它们的几种方法:
<Sandpack>

```js App.js
import {useImmerReducer} from 'use-immer';
import { useImmerReducer } from 'use-immer';
import AddTask from './AddTask.js';
import TaskList from './TaskList.js';

Expand Down Expand Up @@ -969,7 +969,7 @@ const initialTasks = [
```

```js AddTask.js hidden
import {useState} from 'react';
import { useState } from 'react';

export default function AddTask({onAddTask}) {
const [text, setText] = useState('');
Expand All @@ -993,7 +993,7 @@ export default function AddTask({onAddTask}) {
```

```js TaskList.js hidden
import {useState} from 'react';
import { useState } from 'react';

export default function TaskList({tasks, onChangeTask, onDeleteTask}) {
return (
Expand Down Expand Up @@ -1131,10 +1131,10 @@ case 'changed_selection': {
<Sandpack>

```js App.js
import {useReducer} from 'react';
import { useReducer } from 'react';
import Chat from './Chat.js';
import ContactList from './ContactList.js';
import {initialState, messengerReducer} from './messengerReducer';
import { initialState, messengerReducer } from './messengerReducer';

export default function Messenger() {
const [state, dispatch] = useReducer(messengerReducer, initialState);
Expand Down Expand Up @@ -1214,7 +1214,7 @@ export default function ContactList({contacts, selectedId, dispatch}) {
```

```js Chat.js
import {useState} from 'react';
import { useState } from 'react';

export default function Chat({contact, message, dispatch}) {
return (
Expand Down Expand Up @@ -1281,10 +1281,10 @@ dispatch({
<Sandpack>

```js App.js
import {useReducer} from 'react';
import { useReducer } from 'react';
import Chat from './Chat.js';
import ContactList from './ContactList.js';
import {initialState, messengerReducer} from './messengerReducer';
import { initialState, messengerReducer } from './messengerReducer';

export default function Messenger() {
const [state, dispatch] = useReducer(messengerReducer, initialState);
Expand Down Expand Up @@ -1367,7 +1367,7 @@ export default function ContactList({contacts, selectedId, dispatch}) {
```

```js Chat.js
import {useState} from 'react';
import { useState } from 'react';

export default function Chat({contact, message, dispatch}) {
return (
Expand Down Expand Up @@ -1425,10 +1425,10 @@ textarea {
<Sandpack>

```js App.js
import {useReducer} from 'react';
import { useReducer } from 'react';
import Chat from './Chat.js';
import ContactList from './ContactList.js';
import {initialState, messengerReducer} from './messengerReducer';
import { initialState, messengerReducer } from './messengerReducer';

export default function Messenger() {
const [state, dispatch] = useReducer(messengerReducer, initialState);
Expand Down Expand Up @@ -1511,7 +1511,7 @@ export default function ContactList({contacts, selectedId, dispatch}) {
```

```js Chat.js active
import {useState} from 'react';
import { useState } from 'react';

export default function Chat({contact, message, dispatch}) {
return (
Expand Down Expand Up @@ -1564,10 +1564,10 @@ textarea {
<Sandpack>

```js App.js
import {useReducer} from 'react';
import { useReducer } from 'react';
import Chat from './Chat.js';
import ContactList from './ContactList.js';
import {initialState, messengerReducer} from './messengerReducer';
import { initialState, messengerReducer } from './messengerReducer';

export default function Messenger() {
const [state, dispatch] = useReducer(messengerReducer, initialState);
Expand Down Expand Up @@ -1650,7 +1650,7 @@ export default function ContactList({contacts, selectedId, dispatch}) {
```

```js Chat.js active
import {useState} from 'react';
import { useState } from 'react';

export default function Chat({contact, message, dispatch}) {
return (
Expand Down Expand Up @@ -1712,10 +1712,10 @@ textarea {
<Sandpack>

```js App.js
import {useReducer} from 'react';
import { useReducer } from 'react';
import Chat from './Chat.js';
import ContactList from './ContactList.js';
import {initialState, messengerReducer} from './messengerReducer';
import { initialState, messengerReducer } from './messengerReducer';

export default function Messenger() {
const [state, dispatch] = useReducer(messengerReducer, initialState);
Expand Down Expand Up @@ -1804,7 +1804,7 @@ export default function ContactList({contacts, selectedId, dispatch}) {
```

```js Chat.js active
import {useState} from 'react';
import { useState } from 'react';

export default function Chat({contact, message, dispatch}) {
return (
Expand Down Expand Up @@ -1909,10 +1909,10 @@ export const initialState = {
<Sandpack>

```js App.js
import {useReducer} from 'react';
import { useReducer } from 'react';
import Chat from './Chat.js';
import ContactList from './ContactList.js';
import {initialState, messengerReducer} from './messengerReducer';
import { initialState, messengerReducer } from './messengerReducer';

export default function Messenger() {
const [state, dispatch] = useReducer(messengerReducer, initialState);
Expand Down Expand Up @@ -2001,7 +2001,7 @@ export default function ContactList({contacts, selectedId, dispatch}) {
```

```js Chat.js
import {useState} from 'react';
import { useState } from 'react';

export default function Chat({contact, message, dispatch}) {
return (
Expand Down Expand Up @@ -2086,10 +2086,10 @@ Here is the complete solution:
<Sandpack>

```js App.js
import {useReducer} from 'react';
import { useReducer } from 'react';
import Chat from './Chat.js';
import ContactList from './ContactList.js';
import {initialState, messengerReducer} from './messengerReducer';
import { initialState, messengerReducer } from './messengerReducer';

export default function Messenger() {
const [state, dispatch] = useReducer(messengerReducer, initialState);
Expand Down Expand Up @@ -2187,7 +2187,7 @@ export default function ContactList({contacts, selectedId, dispatch}) {
```

```js Chat.js
import {useState} from 'react';
import { useState } from 'react';

export default function Chat({contact, message, dispatch}) {
return (
Expand Down Expand Up @@ -2274,10 +2274,10 @@ export function useReducer(reducer, initialState) {
<Sandpack>

```js App.js
import {useReducer} from './MyReact.js';
import { useReducer } from './MyReact.js';
import Chat from './Chat.js';
import ContactList from './ContactList.js';
import {initialState, messengerReducer} from './messengerReducer';
import { initialState, messengerReducer } from './messengerReducer';

export default function Messenger() {
const [state, dispatch] = useReducer(messengerReducer, initialState);
Expand Down Expand Up @@ -2351,7 +2351,7 @@ export function messengerReducer(state, action) {
```

```js MyReact.js active
import {useState} from 'react';
import { useState } from 'react';

export function useReducer(reducer, initialState) {
const [state, setState] = useState(initialState);
Expand Down Expand Up @@ -2387,7 +2387,7 @@ export default function ContactList({contacts, selectedId, dispatch}) {
```

```js Chat.js hidden
import {useState} from 'react';
import { useState } from 'react';

export default function Chat({contact, message, dispatch}) {
return (
Expand Down Expand Up @@ -2448,10 +2448,10 @@ dispatch 一个 action 去调用一个具有当前 state 和 action 的 reducer
<Sandpack>

```js App.js
import {useReducer} from './MyReact.js';
import { useReducer } from './MyReact.js';
import Chat from './Chat.js';
import ContactList from './ContactList.js';
import {initialState, messengerReducer} from './messengerReducer';
import { initialState, messengerReducer } from './messengerReducer';

export default function Messenger() {
const [state, dispatch] = useReducer(messengerReducer, initialState);
Expand Down Expand Up @@ -2525,7 +2525,7 @@ export function messengerReducer(state, action) {
```

```js MyReact.js active
import {useState} from 'react';
import { useState } from 'react';

export function useReducer(reducer, initialState) {
const [state, setState] = useState(initialState);
Expand Down Expand Up @@ -2564,7 +2564,7 @@ export default function ContactList({contacts, selectedId, dispatch}) {
```

```js Chat.js hidden
import {useState} from 'react';
import { useState } from 'react';

export default function Chat({contact, message, dispatch}) {
return (
Expand Down
1 change: 1 addition & 0 deletions src/content/learn/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ import { useState } from 'react';
```js
function MyButton() {
const [count, setCount] = useState(0);
// ...
```

你将从 `useState` 中获得两样东西:当前的 state(`count`),以及用于更新它的函数(`setCount`)。你可以给它们起任何名字,但按照惯例,需要像这样 `[something, setSomething]` 为它们命名。
Expand Down
2 changes: 1 addition & 1 deletion src/content/learn/manipulating-the-dom-with-refs.md
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ Refs 是一个应急方案。你应该只在你必须“跳出 React”时使用
<Sandpack>

```js
import {useState, useRef} from 'react';
import { useState, useRef } from 'react';

export default function Counter() {
const [show, setShow] = useState(true);
Expand Down
4 changes: 2 additions & 2 deletions src/content/learn/tutorial-tic-tac-toe.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,8 @@ Click on the file labeled `styles.css` in the _Files_ section of CodeSandbox. Th
Click on the file labeled `index.js` in the _Files_ section of CodeSandbox. You won't be editing this file during the tutorial but it is the bridge between the component you created in the `App.js` file and the web browser.

```jsx
import {StrictMode} from 'react';
import {createRoot} from 'react-dom/client';
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import './styles.css';

import App from './App';
Expand Down
Loading