{"id":22928616,"url":"https://github.com/arronf2e/react-lessons","last_synced_at":"2025-04-01T16:18:38.156Z","repository":{"id":123717799,"uuid":"231934621","full_name":"arronf2e/react-lessons","owner":"arronf2e","description":"Created with StackBlitz ⚡️  React Hooks 学习","archived":false,"fork":false,"pushed_at":"2020-01-05T16:53:53.000Z","size":7,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-07T10:28:59.515Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://stackblitz.com/edit/react-s1dvwe","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/arronf2e.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-01-05T15:07:23.000Z","updated_at":"2020-01-05T16:55:14.000Z","dependencies_parsed_at":null,"dependency_job_id":"8b4292b9-d18e-4950-aadb-04a08793ed04","html_url":"https://github.com/arronf2e/react-lessons","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arronf2e%2Freact-lessons","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arronf2e%2Freact-lessons/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arronf2e%2Freact-lessons/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arronf2e%2Freact-lessons/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arronf2e","download_url":"https://codeload.github.com/arronf2e/react-lessons/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246668905,"owners_count":20814744,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-14T09:27:11.466Z","updated_at":"2025-04-01T16:18:38.133Z","avatar_url":"https://github.com/arronf2e.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# react-s1dvwe\n\n[Edit on StackBlitz ⚡️](https://stackblitz.com/edit/react-s1dvwe)\n\n\n# React hooks 学习记录\n\n### Hook 是什么 ？\n\nHook 是一个特殊的函数，它可以让你“钩入” React 的特性。比如 useState 是允许我们在 React function component 中添加 state 的 Hook。从 16.8.0 开始就可以使用稳定的 Hooks 了。\n\n### Hook 使用规则\n\nHook 就是 JavaScript 函数，但是使用它们会有两个额外的规则\n\n- 只能在函数最外层调用 Hook。不要在循环、条件判断或者子函数中调用，是为了确保 Hook 在每一次渲染中都按照同样的顺序被调用，这让 react 能够在多次的 useState 和 useEffect 调用之间保持 hook 状态的正确。\n- 只能在 React 的函数组件中调用 Hook。不要在其他 JavaScript 函数中调用\n\n\n### useState\n\n```js\nconst [count, setCount] = useState(0)\n```\n\n\n### useEffect\n\n```js\nuseEffect(()=\u003e {\n  return () =\u003e {\n    // === componentWillUnmount  ，清除副作用\n  }\n}, [])\n// 第二个参数如果是空数组，该 useEffect 的功能就和 componentDidMount 一样的，我们可以通过第二个参数来做一些性能优化，[]中的如果有 state 的值，那么只有该 state 改变的时候，该“副作用”才执行。\n```\n\n当你调用 useEffect 时，就是在告诉 React 在完成对 DOM 的更改后运行你的“副作用”函数\n\nuseEffect 可以看作是 componentDidMount，componentDidUpdate 和 componentWillUnmount 这三个函数的组合，“副作用函数”可以有多个\n\n\n\n### 自定义 Hook\n\n通过自定义 Hook,可以将组件逻辑提取到可重用的函数中；必须以 use 开头\n\n```js\nfunction useFriendStatus(friendId) {\n  const [isOnline, setIsOnline] = useState(null)\n  // ...\n  return isOnline\n}\n\n// 使用自定义 Hook\nfunction FriendListItem(props) {\n  const isOnline = useFriendStatus(props.friend.id)\n}\n```\n\n### useContext\n\n```js\nconst themes = {\n  light: {\n    foreground: \"#000000\",\n    background: \"#eeeeee\"\n  },\n  dark: {\n    foreground: \"#ffffff\",\n    background: \"#222222\"\n  }\n};\n\nconst ThemeContext = React.createContext(themes.light)\n\nfunction Toolbar(props) {\n  return (\n    \u003cdiv\u003e\n      \u003cThemedButton /\u003e\n    \u003c/div\u003e\n  )\n}\n\nfunction ThemedButton() {\n  const theme = useContext(ThemeContext)\n  return (\n    \u003cbutton style={{\n      background: theme.background, \n      color: theme.foreground\n    }}\u003e\n      I am styled by theme context !\n    \u003c/button\u003e\n  )\n}\n\nfunction ThemeContextApp() {\n  return (\n    \u003cdiv\u003e\n      \u003cThemeContext.Provider value={themes.dark}\u003e\n        \u003cToolbar /\u003e\n      \u003c/ThemeContext.Provider \u003e\n    \u003c/div\u003e\n  )\n}\n\nexport default ThemeContextApp\n```\n\n### useRef\n\n```js\nconst inputEl = useRef(null)\n\nfunction onButtonClick() {\n  inputEl.current.focus()\n}\n\nreturn (\n  \u003c\u003e\n    \u003cinput ref={inputEl} type=\"text\"/\u003e\n    \u003cbutton onClick={onButtonClick}\u003eFocus the input\u003c/button\u003e\n  \u003c/\u003e\n)\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farronf2e%2Freact-lessons","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farronf2e%2Freact-lessons","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farronf2e%2Freact-lessons/lists"}