小编Okt*_*can的帖子

React Vite Typescript 版本 5 无法使用新的装饰器

我使用 vite 创建了新的 React Typescript 应用程序

我执行npm create vite@latest然后选择typescript-swc

  • package.json 中的 Typescript 版本为 ^5.0.2
  • node_modules/typescript/package.json 中的版本是 5.1.3
  • 我的全球版本是5.1.3

我不明白为什么我不能使用新的装饰器

function loggedMethod(originalMethod: any, context: ClassMethodDecoratorContext) {
  console.log(context)
  const methodName = String(context.name)
  function replacementMethod(this: any, ...args: any[]) {
      console.log(`LOG: Entering method '${methodName}'.`)
      const result = originalMethod.call(this, ...args)
      console.log(`LOG: Exiting method '${methodName}'.`)
      return result
  }
  return replacementMethod
}

class Person {
  name: string

  constructor(name: string) {
      this.name = name
  }

  @loggedMethod
  greet() {
      console.log(`Hello, my name is ${this.name}.`)
  }
} …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs typescript-decorator vite vite-reactjs

6
推荐指数
1
解决办法
1977
查看次数

使用 defaultProps 设计的 React Material UI v5

当使用多个样式组件时,最上面的一个会覆盖其他默认的 props。

import { styled } from '@mui/material/styles'
import { Badge } from '@mui/material'


const Badge1 = styled(Badge)``

// this works if Badge1 is used directly: <Badge1 />
Badge1.defaultProps = {
    max: Infinity
}


const Badge2 = styled(Badge1)``    // styled Badge1

// this overrides defaultProps from Badge1. Prop max: Infinity does no apply here
Badge2.defaultProps = {
    variant: 'standard'
}
Run Code Online (Sandbox Code Playgroud)

Badge2 只有变体:“标准”默认道具。它跳过最大值:无穷大

如何保留每个级别的所有默认道具

reactjs material-ui styled-components react-props

4
推荐指数
1
解决办法
3618
查看次数