React Typescript - Adding custom attribute

后端 未结 2 665
失恋的感觉
失恋的感觉 2021-02-13 06:09

React Typescript allow to add custom data-* attributes. But is it possible to add custom attributes like \'name\' || \'test\' act. ?



        
相关标签:
2条回答
  • 2021-02-13 06:31

    in react 16+ it is possible, see

    probem is that typescript didnt know about it(yet)

    but you can still add @ts ignore for typechecking

    { //@ts-ignore
      <span name="I'm causing a type error" data-test="I'm Working"/>
    }
    
    0 讨论(0)
  • 2021-02-13 06:53

    there is another way... skipping the static check (typescript don't do dynamic)

    { 
      const allowedProps = {test: "not-data-attribute"}
      <span {...allowedProps}/>
    }
    
    0 讨论(0)
提交回复
热议问题