{"id":22938610,"url":"https://github.com/szlsay/hello-react","last_synced_at":"2025-07-23T16:09:21.942Z","repository":{"id":184965702,"uuid":"672735229","full_name":"szlsay/hello-react","owner":"szlsay","description":null,"archived":false,"fork":false,"pushed_at":"2023-08-04T01:56:32.000Z","size":3832,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-01T19:42:48.699Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/szlsay.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2023-07-31T03:39:47.000Z","updated_at":"2023-07-31T05:48:59.000Z","dependencies_parsed_at":"2025-02-09T00:31:12.602Z","dependency_job_id":null,"html_url":"https://github.com/szlsay/hello-react","commit_stats":null,"previous_names":["szlsay/hello-react"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/szlsay/hello-react","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szlsay%2Fhello-react","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szlsay%2Fhello-react/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szlsay%2Fhello-react/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szlsay%2Fhello-react/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/szlsay","download_url":"https://codeload.github.com/szlsay/hello-react/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szlsay%2Fhello-react/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266708514,"owners_count":23971946,"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","status":"online","status_checked_at":"2025-07-23T02:00:09.312Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-14T12:19:51.633Z","updated_at":"2025-07-23T16:09:21.889Z","avatar_url":"https://github.com/szlsay.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# hello-react\n前端框架的核心价值: 组件化 和 数据驱动视图\n\n## mock\nnpm init -y\n\n## 3、Hooks\n### useState\n\n### useEffect\n### 其他内置 Hooks - useRef useMemo useCallback\n### 自定义 Hooks\n### 第三方 Hooks\n### Hooks 使用规则\n\n## 2、JSX语法\n标签\n首字母小写 - HTML tag, 首字母大写为自定义组件 如 \u003cinput/\u003e 和 \u003cInput/\u003e 就不一样\nJSX 里的标签必须是闭合的，\u003cinput\u003e \u003cbr\u003e 这样写在 JSX 会报错（在 HTML 中不会报错），必须闭合 \u003cinput/\u003e\n每一段 JSX 只能有一个根节点，或者使用 \u003c\u003e\u003c/\u003e （ Fragment ）\n\n属性\n和 HTML 属性基本一样\nclass 要改为 className\nstyle 要写成 JS 对象（不能是 string），key 采用驼峰写法\nfor 要改为 htmlFor\n\n事件\nonXxx 的形式\n\n注意 TS 的写法\n```\nfunction clickHandler(event: React.MouseEvent\u003cHTMLParagraphElement\u003e) {\n    event.preventDefault()\n    console.log('clicked')\n}\n\nreturn \u003cp onClick={clickHandler}\u003ehello world\u003c/p\u003e\n```\n如果要想传递参数，可以通过如下方式\n```\n  function clickHandler(event: React.MouseEvent\u003cHTMLParagraphElement\u003e, x: string) {\n    event.preventDefault()\n    console.log('clicked', x)\n  }\n\n  return (\n    \u003cp onClick={(e: React.MouseEvent\u003cHTMLParagraphElement\u003e) =\u003e clickHandler(e, 'hello')}\u003e\n        hello world\n    \u003c/p\u003e\n  )\n ```\nPS：Event handlers must be passed, not called! onClick={handleClick}, not onClick={handleClick()}.\n\n## TS练习\nhttps://www.tslang.cn/play/index.html\n\n```\nconst str: string = \"123\";\n\nfunction fn(a: number, b: number) { \n    return a + b;\n}\n\nconsole.log(fn(10, 20))\n\nfunction print\u003cT\u003e(info: T) { \n    console.log(info)\n}\n\nprint(12)\nprint(\"we\")\n\nclass Foo \u003cT\u003e { \n    info: T\n    setInfo(newInfo: T) { \n        this.info = newInfo\n    }\n}\n\nconst foo1: Foo\u003cnumber\u003e = new Foo()\nfoo1.setInfo(12)\nconsole.log(foo1.info)\n\n// foo1.setInfo('ui') // 报错\n// console.log(foo1.info)\n```\n\n### JS 表达式\n{xxx} 格式表示一个 JS 变量或表达式，可用于\n\n普通文本内容，或判断、循环\n属性值\n用于注释\n\n\n### 判断\nJS 一般使用 if...else 做判断，但不能用于 JSX 的 {xxx} 中。\n\n所以，可以选择其他方式做判断\n\n运算符 \u0026\u0026\n三元表达式 a ? b : c\n用函数封装\n```\nconst flag = true\nreturn \u003cdiv\u003e\n    {flag \u0026\u0026 \u003cp\u003ehello\u003c/p\u003e}\n    {flag ? \u003cp\u003e你好\u003c/p\u003e : \u003cp\u003e再见\u003c/p\u003e}\n\u003c/div\u003e\n```\n\n或者用函数封装\n```\nfunction Hello() {\n    if (flag) return \u003cp\u003e你好\u003c/p\u003e\n    else return \u003cp\u003e再见\u003c/p\u003e\n}\n\nreturn \u003cHello\u003e\u003c/Hello\u003e\n```\n\n### 循环\n使用 map 做循环\n```\n  const list = [\n    { username: 'zhangsan', name: '张三' },\n    { username: 'lisi', name: '李四' },\n    { username: 'shuangyue', name: '双越' },\n  ]\n\n  const ul = \u003cul\u003e\n    {list.map(user =\u003e {\n        return \u003cli key={user.username}\u003e{user.name}\u003c/li\u003e\n    })}\n  \u003c/ul\u003e\n```\nJSX 循环必须有 key - 帮助 React 识别哪些元素改变了，比如被添加或删除。\n\n同级别 key 必须唯一\nkey 是不可改变的 —— 尽量不用 index ，要用业务 ID （也不要用随机数）\nkey 用于优化 VDOM diff 算法（后面再说）\n\n## vite安装\nhttps://cn.vitejs.dev/\n\nnpm create vite@latest react-demo-vite --template react-ts\n\n## 1、安装\n### 安装node.js\nhttps://nodejs.org/en\n\n### Create React App 中文文档\nhttps://create-react-app.bootcss.com/\n\n### react官网\nhttps://react.dev/\nhttps://zh-hans.react.dev/ \n\n### 安装命令\nnpx create-react-app react-ts-demo --template typescript\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fszlsay%2Fhello-react","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fszlsay%2Fhello-react","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fszlsay%2Fhello-react/lists"}