{"id":20355610,"url":"https://github.com/geekeast/react-hooks-examples","last_synced_at":"2025-03-04T17:30:04.505Z","repository":{"id":39250470,"uuid":"236422361","full_name":"GeekEast/react-hooks-examples","owner":"GeekEast","description":"React hooks examples","archived":false,"fork":false,"pushed_at":"2023-01-05T05:48:17.000Z","size":5042,"stargazers_count":1,"open_issues_count":24,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-02T12:17:27.966Z","etag":null,"topics":["inblog","memo","usecallback","usecontext","useeffect","uselayouteffect","usememo"],"latest_commit_sha":null,"homepage":"https://codesandbox.io/s/github/GeekEast/react-hooks-examples","language":"TypeScript","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/GeekEast.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}},"created_at":"2020-01-27T04:58:43.000Z","updated_at":"2020-04-19T01:45:26.000Z","dependencies_parsed_at":"2023-02-03T14:15:44.950Z","dependency_job_id":null,"html_url":"https://github.com/GeekEast/react-hooks-examples","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/GeekEast%2Freact-hooks-examples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GeekEast%2Freact-hooks-examples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GeekEast%2Freact-hooks-examples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GeekEast%2Freact-hooks-examples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GeekEast","download_url":"https://codeload.github.com/GeekEast/react-hooks-examples/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241889315,"owners_count":20037488,"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":["inblog","memo","usecallback","usecontext","useeffect","uselayouteffect","usememo"],"created_at":"2024-11-14T23:13:27.527Z","updated_at":"2025-03-04T17:30:04.471Z","avatar_url":"https://github.com/GeekEast.png","language":"TypeScript","readme":"### variable\n |        特性        | `plain variable` | `state` | `ref` |\n | :----------------: | :--------------: | :-----: | :---: |\n | 渲染后引用是否相同 |        ❎         |    ❎    |   ✔   |\n |   是否可以update   |        ❎         |    ✔    |   ✔   |\n\n### useContext vs redux\n- useContext的变化会引起中间组件的重新渲染\n- store的变化只会引起相关组件的重新渲染\n\n### useRef\n- 持久化的引用，只有`current`可用\n- 可以用来获取或者添加`dom node`\n\n### useMemo vs useCallback vs memo\n- `useMemo`用来**缓存**`昂贵计算`的**值**, 也可以缓存`组件`\n- `useCallback`用来**持久化**函数的`引用`\n- `memo`用来缓存`组件`, 可以避免由`父组件渲染`引起的**不必要**的`子组件渲染`\n\n\n### useEffect vs useLayoutEffect\n\u003cdiv style=\"text-align:center; margin:auto\"\u003e\u003cimg src=\"img/2020-01-27-10-16-05.png\"\u003e\u003c/div\u003e\n\n- [useEffect 和 useLayoutEffect 的区别](https://juejin.im/post/5de38c76e51d455f9b335eff)\n- [react hook——你可能不是“我”所认识的useEffect](https://imweb.io/topic/5cd845cadcd62f86299fcd76)\n\n\n### useContext\n- 创建context\n  - defaultContext是在没有provider的情况下，传给consumer或者useContext的默认值，但是是不可变的，无法通过setContext来变更。\n```javascript\n// userContext.js\nimport { createContext } from 'react';\n\nconst UserContext = createContext([\n  {\n    firstName: 'Bob',\n    lastName: 'Bobberson',\n    suffix: 1,\n    email: 'bobberson@example.com'\n  },\n  obj =\u003e obj\n])\nconst { Provider } = UserContext;\nexport { Provider, UserContext }\n```\n- 创建Provider\n```javascript\nconst UseContext = () =\u003e {\n  // 这个才是人类理解的默认值，可通过setContext来变更\n  const user = useState({\n    firstName: 'James',\n    lastName: 'Tan',\n    suffix: 1,\n    email: 'james@example.com'\n  })\n\n  return (\n    \u003cdiv\u003e\n      \u003cProvider value={user}\u003e\n        \u003ch1\u003e1st level\u003c/h1\u003e\n        \u003cLevel2\u003e\u003c/Level2\u003e\n      \u003c/Provider\u003e\n    \u003c/div\u003e\n    //  \n  )\n}\n```\n- 创建Consumer\n```javascript\nconst Level5 = () =\u003e {\n  // 接收context\n  const [user, setUser] = useContext\u003cany\u003e(UserContext);\n  return (\n    \u003cdiv\u003e\n      \u003ch5\u003e{`${user.firstName} ${user.lastName} the ${user.suffix} born`}\u003c/h5\u003e\n      \u003cbutton onClick={() =\u003e { setUser({ ...user, suffix: user.suffix + 1 }) }}\u003eIncrement\u003c/button\u003e\n    \u003c/div\u003e\n  )\n}\n```\n\n### forwardRef\n- 能够跟`useRef`一起获得子组件的`DOM`\n- 在`父组件`创建`ref`,传递到`子组件`, 在`父组件`获取`DOM`\n```javascript\nconst FancyButton = React.forwardRef((props, ref) =\u003e (\n  \u003cbutton ref={ref} className=\"FancyButton\"\u003e\n    {props.children}\n  \u003c/button\u003e\n));\n\n// You can now get a ref directly to the DOM button:\nconst ref = React.createRef();\n\u003cFancyButton ref={ref}\u003eClick me!\u003c/FancyButton\u003e;\n```\n\n### useImperativeHandle\n- 结合`useRef`和`forwardRef`, 允许`子组件`将操作**自身DOM**的`函数`传递给`父组件`(向上传递)\n\n### 零碎\n- `htmlFor` for `for`\n- `Context` + `useReducer` = `Redux`; 其实还是redux的方案更好，context其实只是props多层传递的一种简化而已。\n- `styled-component`: `emotion.sh` 将css复用粒度提升tag层面到了component层面，但是加快了开发速度，可以适用于小型项目。\n- `styled-component`高亮插件: `vscode-styled-component`\n- `sass`: `yarn add node-sass`\n- `setState`: hooks是function之外的又一层，写在代码里的顺序，不一定是它真实的执行顺序\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeekeast%2Freact-hooks-examples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgeekeast%2Freact-hooks-examples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeekeast%2Freact-hooks-examples/lists"}