Skip to content

Commit 75f0d3f

Browse files
committed
update part5b: synchronized changes in english material
1 parent c1b963a commit 75f0d3f

File tree

1 file changed

+70
-201
lines changed

1 file changed

+70
-201
lines changed

src/content/5/zh/part5b.md

Lines changed: 70 additions & 201 deletions
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,9 @@ const App = () => {
457457
我们还对<i>Togglable</i>组件做了如下修改。
458458

459459
```js
460-
import { useState, forwardRef, useImperativeHandle } from 'react' // highlight-line
460+
import { useState, useImperativeHandle } from 'react' // highlight-line
461461

462-
const Togglable = forwardRef((props, ref) => { // highlight-line
462+
const Togglable = (props) => { // highlight-line
463463
const [visible, setVisible] = useState(false)
464464

465465
const hideWhenVisible = { display: visible ? 'none' : '' }
@@ -470,10 +470,8 @@ const Togglable = forwardRef((props, ref) => { // highlight-line
470470
}
471471

472472
// highlight-start
473-
useImperativeHandle(ref, () => {
474-
return {
475-
toggleVisibility
476-
}
473+
useImperativeHandle(props.ref, () => {
474+
return { toggleVisibility }
477475
})
478476
// highlight-end
479477

@@ -488,15 +486,11 @@ const Togglable = forwardRef((props, ref) => { // highlight-line
488486
</div>
489487
</div>
490488
)
491-
}) // highlight-line
489+
}
492490

493491
export default Togglable
494492
```
495493

496-
497-
<!-- The function that creates the component is wrapped inside of a [forwardRef](https://reactjs.org/docs/react-api.html#reactforwardref) function call. This way the component can access the ref that is assigned to it.-->
498-
创建该组件的函数被包裹在一个[forwardRef](https://reactjs.org/docs/react-api.html#reactforwardref)函数调用中。这样,组件就可以访问分配给它的Ref。
499-
500494
<!-- The component uses the [useImperativeHandle](https://reactjs.org/docs/hooks-reference.html#useimperativehandle) hook to make its <i>toggleVisibility</i> function available outside of the component.-->
501495
该组件使用[useImperativeHandle](https://reactjs.org/docs/hooks-reference.html#useimperativehandle)钩子来使它的<i>toggleVisibility</i>函数在组件之外可用。
502496

@@ -592,8 +586,8 @@ const Togglable = () => ...
592586

593587
![](../../images/5/13be.png)
594588

595-
<!-- The form closes when a new blog is created.-->
596-
当一个新的博客被创建时,该表单就会关闭。
589+
<!-- The form hides again after a new blog is created or the <i>cancel</i> button is pressed.-->
590+
当一个新的博客被创建或点击取消按钮后时,该表单就会关闭。
597591

598592
#### 5.6 Blog list frontend, step6
599593

@@ -725,209 +719,84 @@ const Blog = ({ blog }) => {
725719

726720
<div class="content">
727721

728-
### PropTypes
729-
730-
<!-- The <i>Togglable</i> component assumes that it is given the text for the button via the <i>buttonLabel</i> prop. If we forget to define it to the component:-->
731-
<i>Togglable</i>组件假定它通过<i>buttonLabel</i>prop得到了按钮的文本。如果我们忘记向组件定义它。
732-
733-
```js
734-
<Togglable> buttonLabel forgotten... </Togglable>
735-
```
736-
737-
<!-- The application works, but the browser renders a button that has no label text.-->
738-
应用可以工作,但浏览器显示的按钮没有标签文本。
739-
740-
<!-- We would like to enforce that when the <i>Togglable</i> component is used, the button label text prop must be given a value.-->
741-
我们希望强制规定,当使用<i>Togglable</i>组件时,必须给按钮标签文本prop一个值。
742-
743-
<!-- The expected and required props of a component can be defined with the [prop-types](https://github.com/facebook/prop-types) package. Let's install the package:-->
744-
组件的预期和要求的prop可以用[prop-types](https://github.com/facebook/prop-types)包来定义。让我们安装这个包。
745-
746-
```shell
747-
npm install prop-types
748-
```
749-
750-
<!-- We can define the <i>buttonLabel</i> prop as a mandatory or <i>required</i> string-type prop as shown below:-->
751-
我们可以把<i>buttonLabel</i>prop定义为强制或<i>required</i>字符串型prop,如下所示。
752-
753-
```js
754-
import PropTypes from 'prop-types'
755-
756-
const Togglable = React.forwardRef((props, ref) => {
757-
// ..
758-
})
759-
760-
Togglable.propTypes = {
761-
buttonLabel: PropTypes.string.isRequired
762-
}
763-
```
764-
765-
<!-- The console will display the following error message if the prop is left undefined:-->
766-
如果该prop未被定义,控制台将显示以下错误信息。
767-
768-
![](../../images/5/15.png)
769-
770-
771-
<!-- The application still works and nothing forces us to define props despite the PropTypes definitions. Mind you, it is extremely unprofessional to leave <i>any</i> red output to the browser console.-->
772-
尽管有PropTypes的定义,应用仍然可以工作,没有任何东西强迫我们定义prop。请注意,给浏览器控制台留下<i>任何</i>红色输出是非常不专业的。
773-
774-
<!-- Let's also define PropTypes to the <i>LoginForm</i> component:-->
775-
我们也给<i>LoginForm</i>组件定义PropTypes。
776-
777-
```js
778-
import PropTypes from 'prop-types'
779-
780-
const LoginForm = ({
781-
handleSubmit,
782-
handleUsernameChange,
783-
handlePasswordChange,
784-
username,
785-
password
786-
}) => {
787-
// ...
788-
}
789-
790-
LoginForm.propTypes = {
791-
handleSubmit: PropTypes.func.isRequired,
792-
handleUsernameChange: PropTypes.func.isRequired,
793-
handlePasswordChange: PropTypes.func.isRequired,
794-
username: PropTypes.string.isRequired,
795-
password: PropTypes.string.isRequired
796-
}
797-
```
798-
799-
<!-- If the type of a passed prop is wrong, e.g. if we try to define the <i>handleSubmit</i> prop as a string, then this will result in the following warning:-->
800-
如果传递的prop的类型是错误的,例如,如果我们试图将<i>handleSubmit</i>prop定义为字符串,那么这将导致以下警告。
801-
802-
![](../../images/5/16.png)
803-
804722
### ESlint
805723

806724
<!-- In part 3 we configured the [ESlint](/en/part3/validation_and_es_lint#lint) code style tool to the backend. Let's take ESlint to use in the frontend as well.-->
807725
在第三章节,我们将[ESlint](/zh/part3/es_lint与代码检查#lint)代码风格工具配置到后端。让我们把ESlint也用在前端。
808726

809-
<!-- Vite has installed ESlint to the project by default, so all that's left for us to do is define our desired configuration in the <i>.eslintrc.cjs</i> file. -->
810-
Vite 默认将 ESlint 安装到项目中,所以我们剩下要做的就是在 .eslintrc.cjs 文件中定义我们想要的配置。
811-
812-
<!-- Let's create a <i>.eslintrc.cjs</i> file with the following contents: -->
813-
让我们创建一个包含以下内容的 .eslintrc.cjs 文件:
814-
```bash
815-
npm install --save-dev eslint-plugin-jest
816-
```
727+
<!-- Vite has installed ESlint to the project by default, so all that's left for us to do is define our desired configuration in the <i>eslint.config.js</i> file. -->
728+
Vite 默认将 ESlint 安装到项目中,所以我们剩下要做的就是在 <i>eslint.config.js</i> 文件中定义我们想要的配置。
817729

818-
<!-- Let's create a <i>.eslintrc.js</i> file with the following contents:-->
819-
让我们创建一个<i>.eslintrc.js</i>文件,内容如下。
730+
<!-- Let's create a <i>eslint.config.js</i> file with the following contents: -->
731+
让我们创建一个包含以下内容的 <i>eslint.config.js</i> 文件:
820732

821733
```js
822-
module.exports = {
823-
root: true,
824-
env: {
825-
browser: true,
826-
es2020: true,
827-
},
828-
extends: [
829-
'eslint:recommended',
830-
'plugin:react/recommended',
831-
'plugin:react/jsx-runtime',
832-
'plugin:react-hooks/recommended',
833-
],
834-
ignorePatterns: ['dist', '.eslintrc.cjs'],
835-
parserOptions: { ecmaVersion: 'latest', sourceType: 'module' },
836-
settings: { react: { version: '18.2' } },
837-
plugins: ['react-refresh'],
838-
rules: {
839-
"indent": [
840-
"error",
841-
2
842-
],
843-
"linebreak-style": [
844-
"error",
845-
"unix"
846-
],
847-
"quotes": [
848-
"error",
849-
"single"
850-
],
851-
"semi": [
852-
"error",
853-
"never"
854-
],
855-
"eqeqeq": "error",
856-
"no-trailing-spaces": "error",
857-
"object-curly-spacing": [
858-
"error", "always"
859-
],
860-
"arrow-spacing": [
861-
"error", { "before": true, "after": true }
862-
],
863-
"no-console": 0,
864-
"react/react-in-jsx-scope": "off",
865-
"react/prop-types": 0,
866-
"no-unused-vars": 0
867-
},
868-
}
869-
```
870-
871-
<!-- NOTE: If you are using Visual Studio Code together with ESLint plugin, you might need to add additional workspace setting for it to work. If you are seeing ```Failed to load plugin react: Cannot find module 'eslint-plugin-react'``` additional configuration is needed. Adding the line ```"eslint.workingDirectories": [{ "mode": "auto" }]``` to settings.json in the workspace seems to work. See [here](https://github.com/microsoft/vscode-eslint/issues/880#issuecomment-578052807) for more information.-->
872-
注意:如果你将Visual Studio Code与ESLint插件一起使用,你可能需要添加额外的工作区设置,以便它能够工作。如果你看到 ```加载插件reaction失败。无法找到模块"eslint-plugin-react"``` 需要额外的配置。添加一行 ```"eslint.workingDirectories":[{ "mode": "auto" }]``` 到工作区的settings.json中,似乎可以工作。更多信息见[这里](https://github.com/microsoft/vscode-eslint/issues/880#issuecomment-578052807)
734+
import js from '@eslint/js'
735+
import globals from 'globals'
736+
import reactHooks from 'eslint-plugin-react-hooks'
737+
import reactRefresh from 'eslint-plugin-react-refresh'
873738

874-
<!-- Let's create [.eslintignore](https://eslint.org/docs/user-guide/configuring#ignoring-files-and-directories) file with the following contents to the repository root-->
875-
让我们创建[.eslintignore](https://eslint.org/docs/user-guide/configuring#ignoring-files-and-directories)文件,在版本库根目录中加入以下内容
876-
877-
```bash
878-
node_modules
879-
dist
880-
.eslintrc.cjs
881-
vite.config.js
739+
export default [
740+
{ ignores: ['dist'] },
741+
{
742+
files: ['**/*.{js,jsx}'],
743+
languageOptions: {
744+
ecmaVersion: 2020,
745+
globals: globals.browser,
746+
parserOptions: {
747+
ecmaVersion: 'latest',
748+
ecmaFeatures: { jsx: true },
749+
sourceType: 'module'
750+
}
751+
},
752+
plugins: {
753+
'react-hooks': reactHooks,
754+
'react-refresh': reactRefresh
755+
},
756+
rules: {
757+
...js.configs.recommended.rules,
758+
...reactHooks.configs.recommended.rules,
759+
'no-unused-vars': ['error', { varsIgnorePattern: '^[A-Z_]' }],
760+
'react-refresh/only-export-components': [
761+
'warn',
762+
{ allowConstantExport: true }
763+
// highlight-start
764+
],
765+
indent: ['error', 2],
766+
'linebreak-style': ['error', 'unix'],
767+
quotes: ['error', 'single'],
768+
semi: ['error', 'never'],
769+
eqeqeq: 'error',
770+
'no-trailing-spaces': 'error',
771+
'object-curly-spacing': ['error', 'always'],
772+
'arrow-spacing': ['error', { before: true, after: true }],
773+
'no-console': 'off'
774+
//highlight-end
775+
}
776+
}
777+
]
882778
```
883779

884-
<!-- Now the directories <em>build</em> and <em>node_modules</em> will be skipped when linting.-->
885-
现在目录<em>build</em>和<em>node_modules</em>将在检查时被跳过。
886-
887-
<!-- Let us also create a npm script to run the lint:-->
888-
让我们也创建一个npm脚本来运行lint。
780+
<!-- NOTE: If you are using Visual Studio Code together with ESLint plugin, you might need to add a workspace setting for it to work. If you are seeing <i>Failed to load plugin react: Cannot find module 'eslint-plugin-react'</i> additional configuration is needed. Adding the following line to settings.json may help: -->
781+
注意:如果你在 Visual Studio Code 里使用 ESLint 插件,你可能需要修改一些工作区设置。如果你看到 <i>Failed to load plugin react: Cannot find module 'eslint-plugin-react'</i>,那么你需要添加额外的配置。把下一行到添加到 setting.json 中可能会有帮助:
889782

890783
```js
891-
{
892-
// ...
893-
{
894-
"scripts": {
895-
"start": "react-scripts start",
896-
"build": "react-scripts build",
897-
"test": "react-scripts test",
898-
"eject": "react-scripts eject",
899-
"server": "json-server -p3001 db.json",
900-
"eslint": "eslint ." // highlight-line
901-
},
902-
// ...
903-
}
784+
"eslint.workingDirectories": [{ "mode": "auto" }]
904785
```
905786

906-
<!-- Component _Togglable_ causes a nasty looking warning <i>Component definition is missing display name</i>:-->
907-
组件_Togglable_导致一个看起来很讨厌的警告 <i> 组件定义缺少显示名称</i>。
908-
909-
![](../../images/5/25x.png)
787+
<!-- See [here](https://github.com/microsoft/vscode-eslint/issues/880#issuecomment-578052807) for more information. -->
788+
更多信息见[这里](https://github.com/microsoft/vscode-eslint/issues/880#issuecomment-578052807)
910789

911-
<!-- The react-devtools also reveals that the component does not have name:-->
912-
react-devtools也显示出该组件没有名字。
790+
<!-- As usual, you can perform the linting either from the command line with the command -->
791+
通常来说,为了运行 lint,你既可以使用命令行的命令
913792

914-
![](../../images/5/26ea.png)
915-
916-
<!-- Fortunately this is easy to fix-->
917-
幸运的是,这很容易解决
918-
919-
```js
920-
import { useState, useImperativeHandle } from 'react'
921-
import PropTypes from 'prop-types'
922-
923-
const Togglable = React.forwardRef((props, ref) => {
924-
// ...
925-
})
793+
```bash
794+
npm run lint
795+
```
926796

927-
Togglable.displayName = 'Togglable' // highlight-line
797+
<!-- or using the editor's Eslint plugin. -->
928798

929-
export default Togglable
930-
```
799+
也可以使用编辑器的 ESlint 插件。
931800

932801
<!-- You can find the code for our current application in its entirety in the <i>part5-7</i> branch of [this GitHub repository](https://github.com/fullstack-hy2020/part2-notes/tree/part5-7).-->
933802
你可以在[这个github仓库](https://github.com/fullstack-hy2020/part2-notes/tree/part5-7)的<i>part5-7</i>分支中找到我们当前应用的全部代码。
@@ -940,10 +809,10 @@ export default Togglable
940809

941810
#### 5.12: Blog List Frontend, step 12
942811

943-
<!-- Define PropTypes for one of the components of your application, and add ESlint to the project. Define the configuration according to your liking. Fix all of the linter errors. -->
944-
为你的应用程序的一个组件定义 PropTypes,并将 ESlint 添加到项目中。根据你的喜好定义配置。修复所有的 linter 错误。
812+
<!-- Add ESlint to the project. Define the configuration according to your liking. Fix all of the linter errors. -->
813+
ESlint 添加到项目中。根据你的喜好定义配置。然后修复所有的 linter 错误。
945814

946-
<!-- Vite has installed ESlint to the project by default, so all that's left for you to do is define your desired configuration in the <i>.eslintrc.cjs</i> file. -->
947-
Vite 已经默认在项目中安装了 ESlint,所以你需要做的就是在 <i>.eslintrc.cjs</i> 文件中定义你想要的配置。
815+
<!-- Vite has installed ESlint to the project by default, so all that's left for you to do is define your desired configuration in the <i>eslint.config.js</i> file. -->
816+
Vite 已经默认在项目中安装了 ESlint,所以你需要做的就是在 <i>eslint.config.js</i> 文件中定义你想要的配置。
948817

949818
</div>

0 commit comments

Comments
 (0)