【问题标题】:only refers to a type, but is being used as a value here仅指一种类型,但在此处用作值
【发布时间】:2018-08-07 06:58:48
【问题描述】:

我在编写这个 React Native 应用程序时使用了 TypeScript。 尝试创建一个简单的操作,将字符串作为参数并更新状态。但是我得到了这个我不明白的错误。

src/containers/DamageClaimReceipt.tsx

import * as React from "react"
import { Text, View } from "react-native"
import { Question } from "../components/Question"
import GjeButton from "../components/GjeButton"
import { bindActionCreators, Dispatch } from "redux"
import { connect } from "react-redux"
import {
  SetDamageClaimReceiptNrAction
} from "../actions/processguide"

const mapDispatchToProps = (dispatch: Dispatch<any>) => bindActionCreators({
  setDamageClaimReceiptNr: SetDamageClaimReceiptNrAction,
  // 'SetDamageClaimReceiptNrAction' only refers to a type, 
  // but is being used as a value here.
}, dispatch)

class DamageClaimReceipt extends React.Component<any, any> {
  public render() {
    return (
      <View>

       <GjeButton
        title={"Set DamageClaimReceiptNr"}
        onPress={ () => this.props.setDamageClaimReceiptNr("100") }
      />

      </View>
    )
  }
}

export default connect<null, null>(
  null,
  mapDispatchToProps,
)(DamageClaimReceipt)

src/actions/processguide.ts

 10 export type SetDamageClaimReceiptNrAction =
 11   (damageClaimReceiptNr: string) => ISetDamageClaimReceiptNrAction
 12
 13 export interface ISetDamageClaimReceiptNrAction {
 14   type: actionTypes.SET_PROCESSGUIDE_RECEIPT_NR_FOR_CLAIM
 15   receiptNrForClaim: string
 16 }

【问题讨论】:

    标签: reactjs typescript react-native redux react-redux


    【解决方案1】:

    SetDamageClaimReceiptNrAction定义了一个方法签名,当调用bindActionCreators时你传递一个对象字面量给方法,带有setDamageClaimReceiptNr属性,该属性不能有函数签名的值,它可以是一个函数但是。

    const mapDispatchToProps = (dispatch: Dispatch<any>) => bindActionCreators({
      setDamageClaimReceiptNr: (damageClaimReceiptNr: string) => ({
           type: actionTypes.SET_PROCESSGUIDE_RECEIPT_NR_FOR_CLAIM
           receiptNrForClaim: "Value"
      }),
    }, dispatch)
    

    【讨论】:

      猜你喜欢
      • 2020-04-28
      • 2021-08-23
      • 2020-12-05
      • 2018-03-31
      • 1970-01-01
      • 2023-04-03
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多