Skip to content

Instantly share code, notes, and snippets.

@HiChen404

HiChen404/React Secret

Last active September 3, 2022 12:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HiChen404/850328bbf20fe27062799ea7d8703b1f to your computer and use it in GitHub Desktop.
Save HiChen404/850328bbf20fe27062799ea7d8703b1f to your computer and use it in GitHub Desktop.
React
const a = 1
@HiChen404
Copy link
Author

HiChen404 commented May 15, 2022

#React #Emotin

Emotion使用行内 CSS 属性时,如果遇到错误:

pragma and pragmaFrag cannot be set when runtime is automatic.

可以修改:tsconfig

"types": ["@emotion/react/types/css-prop"],

"jsx": "react-jsx",

"jsxImportSource": "@emotion/react"

可参考一下解决办法:

https://stackoverflow.com/questions/66965774/emotion-css-prop-and-nextjs-new-jsx-runtime-error-pragma-and-pragmafrag-canno

@HiChen404
Copy link
Author

React 中使用 SVG

import { ReactComponent as SoftwareLogo } from "assets/software-logo.svg"

<SoftwareLogo width={"18rem"} color={"rgb(12,138,255)"} />

@HiChen404
Copy link
Author

HiChen404 commented May 15, 2022

#Emotion

HTML Element pass Custom Property

Emotion 自定义 HTML 元素中使用自定义属性,报错解决办法:

"types": ["@emotion/react/types/css-prop"]

and :定义泛型

image

@HiChen404
Copy link
Author

HiChen404 commented May 15, 2022

#QA

How to fix missing dependency warning when using useEffect React Hook

如何忽略 Hook 中依赖缺失的提示信息:

useEffect(() => {
   // other code
   ...

   // eslint-disable-next-line react-hooks/exhaustive-deps
}, []) 

@HiChen404
Copy link
Author

#Bug

这种情况会造成循环渲染,因为 obj 被重复定义:

import React, { useState, useEffect } from "react"

export default function App() {
  const obj = { name: "Jack" }
  const [num, setNum] = useState(0)

  useEffect(() => {
    console.log("useEffect")
    setNum(num + 1)
  }, [obj])

  return <h1>{num}</h1>
}

但如果 obj 为基本类型,则不会造成无限循环。

@HiChen404
Copy link
Author

#TS

例:获取 AntD Select 组件的所有属性:

type SelectProps = React.ComponentProps<typeof Select>

@HiChen404
Copy link
Author

/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react'
import styled from '@emotion/styled'

@HiChen404
Copy link
Author

HiChen404 commented Jun 7, 2022

#Bug

关于 React 或 Vue 的部署问题

在部署上线后, 直接访问 https://xxx.com/about 或在 https://xxx.com/about 页面刷新会显示 404 错误。

这是因为如果 SPA 应用使用 history pushState ,那么必须添加一个重写规则。

解决方法:

  • 使用 Hash Router
  • 添加重写规则:
    Netlify: 在 public 目录中创建 _redirects 文件并写入 /* /index.html 200

https://docs.netlify.com/configure-builds/javascript-spas/#build-configuration-for-javascript-spas
https://stackoverflow.com/questions/58065603/netlify-renders-404-on-page-refresh-using-react-and-react-router

@HiChen404
Copy link
Author

HiChen404 commented Jun 9, 2022

#Tips

React 中使用 React.componentProps<T> 获取一个组件其属性的类型。

@HiChen404
Copy link
Author

HiChen404 commented Aug 4, 2022

#Bug #Tips

项目中遇到 ESlint 意料之中的报错,导致无法提交等情况时,可以考虑使用:

// eslint-disable-next-line 
//@ts-ignore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment