Reactj, typescript Property 'setState' is missing in type

前端 未结 2 360
星月不相逢
星月不相逢 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 12:05

    Here's an example of how to use the state and render the components:

    type HomeProps = {
        text: string;
    }
    class Home extends React.Component {
        public render() {
            return 

    { this.props.text }

    } } type AppState = { homeText: string; } class App extends React.Component { constructor() { super(); this.state = { homeText: "home" }; setTimeout(() => { this.setState({ homeText: "home after change "}); }, 1000); } render() { return } }

    As you can see, the props and state objects are always simple, and the rendering method is in charge of creating the actual components.
    This way react knows which components are changed and which parts of the DOM tree should be updated.

提交回复
热议问题