Reactj, typescript Property 'setState' is missing in type

前端 未结 2 354
星月不相逢
星月不相逢 2021-01-17 11:11

I\'m getting a ts error from my react component. The component is running fine, building etc, however typescript is showing an error inside the ide. Not sure how i need to d

2条回答
  •  遥遥无期
    2021-01-17 11:48

    Had the same issue, but my problem was that I was importing React wrong.

    The correct way to import it when using TypeScript is with import * as React from "react".

    Code example:

    import * as React from "react"
    import ReactDOM from "react-dom"
    
    class App extends React.Component {
      render() {
        return (
          
    Hello, World!
    ) } } ReactDOM.render(, document.getElementById("app"))

    Note: allows you to use any props and state variables in your React component, but you should usually define those yourself to take advantage of TypeScript's type annotations.

提交回复
热议问题