super got strikethrough in React

后端 未结 4 744
挽巷
挽巷 2021-01-18 23:52

Code

import React, { Component } from \'react\';
import PropTypes from \'prop-types\';
import axios from \'axios\';
import { connect } from \'react-redux\';         


        
4条回答
  •  不思量自难忘°
    2021-01-19 00:41

    IllusionMH posted this solution in this GitHub thread. You can add

    /** @param {Record} props */
    

    above the constructor function as follows:

    class Example extends React.Component {
        /** @param {Record} props */
        constructor(props) {
            super(props);
        
            console.log(props.message);
            console.log(this.props.message);
        }
        // ...
    }
    

    I am not a TypeScript expert so cannot explain exactly why it works, I assume there is a type definition somewhere that is not handling the constructor or super function correctly and the comment overrides it and tells Typescript to allow the super function to override it and accept the props parameter, similar to how !important tells CSS to give a style precedence or //prettier-ignore tells prettier to ignore the next line of code though I am sure someone more knowledgeable than myself can give a more detailed or accurate explanation.

提交回复
热议问题