【问题标题】:"<Class> is declared but its value never read" and "Cannot find name <Class>" at same time“<Class> 已声明但其值从未读取”和“找不到名称 <Class>”同时
【发布时间】:2018-10-22 19:04:20
【问题描述】:

我正在使用 React 和 Typescript 以及 react-router - 我想使用 RouteComponentProps 自动绑定 this.props.history。这是我的代码:

import * as React from "react";
import { connect } from "react-redux";
import {
    RouteComponentProps,
    withRouter,
} from "react-router-native";
import Example from "../../screens/Example";

interface Props {}

interface State {}

class ExampleContainer extends React.Component<RouteComponentProps & Props, State> {

    isValid(value): void {
      if (value!=="") {
        this.props.history.push('/tasks')
      }
    }

    render() {
        return (
            <Example callback={(value: string) => this.isValid(value)}  />
        );
    }
}

const mapDispatchToProps = (_) => ({});
const mapStateToProps = (_) => ({});
export default withRouter(connect(mapStateToProps, mapDispatchToProps)(ExampleContainer));

但是,我收到一个奇怪的编译器错误,告诉我从未使用过RouteComponentProps,但同时它没有被定义。这里是确切的错误信息:

src/containers/ExampleContainer/index.tsx:4:2 - error TS6133: 'RouteComponentProps' is declared but its value is never read.

4  RouteComponentProps,
   ~~~~~~~~~~~~~~~~~~~

src/containers/ExampleContainer/index.tsx:13:48 - error TS2304: Cannot find name 'RouteComponentProps'.

13 class ExampleContainer extends React.Component<RouteComponentProps & Props, State> {
                                                  ~~~~~~~~~~~~~~~~~~~

如何正确绑定react-router 道具?如果我做错了,我相信我至少应该得到某种TypeError,但我无法理解这个错误...... 我感谢任何帮助指出错误来源的帮助。提前致谢!

【问题讨论】:

    标签: reactjs typescript react-native react-router


    【解决方案1】:

    我通过修改修复了它

    import {
        RouteComponentProps,
        withRouter,
    } from "react-router-native";
    

    import { withRouter } from "react-router-native";
    import { RouteComponentProps }  from "react-router";
    

    【讨论】:

      【解决方案2】:

      我已经通过向.eslintrc 添加忽略模式解决了这个问题:

      "no-unused-vars": [1, {
        "args": "none",
        "varsIgnorePattern": "RouteComponentProps"
      }],
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-08-10
        • 2018-09-28
        • 1970-01-01
        • 2019-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多