在将prop传递给React中的子组件时出现TypeScript错误

Abdul Rehman Aziz |

我正在使用React + Typescript构建费用跟踪器应用

ensemum_type.ts

    export  type IState = {
    id : number,
    text : string,
    amount : number

}

export type IinitialStateType =  {
    transactions : IState[]
}

export type IActions =  {
    type : string,
    payload : any
}

export type contextProps = {
    transactions : IState[];
    addTransaction: (trans: IState) => void;
    deleteTransaction: (id: number) => void;
}

然后,我使用了API上下文和reducer,并在Global State文件中初始化了我的初始状态

GlobalState.tsx

import React , {createContext,useReducer} from 'react'
import {IinitialStateType,contextProps} from '../Types/expense_type'
import AppReducer from './AppReducer'

const initailState : IinitialStateType = {
    transactions : [
          { id: 1, text: 'Flower', amount: -20 },
          { id: 2, text: 'Salary', amount: 300 },
          { id: 3, text: 'Book', amount: -10 },
          { id: 4, text: 'Camera', amount: 150 }
    ]
}

export const GlobalContext = createContext<Partial<contextProps>>({})

export const GlobalProvider : React.FC = ({children}) : JSX.Element => {

    const [state,dispatch] = useReducer(AppReducer,initailState)
    
    return(
        <GlobalContext.Provider value={{
            transactions : state.transactions
        }}>
            {children}
        </GlobalContext.Provider>
    )
}

然后,我在TransactionList.tsx组件中使用了上下文,并映射了所有初始状态值,并将这些值传递给接收这些值的子组件Transaction.tsx

TransactionList.tsx

import React ,{useContext}from 'react'
import {GlobalContext} from './../Context/GlobalState'
import {contextProps,IinitialStateType,IState} from '../Types/expense_type'
import {Transaction} from './Transaction'

export const TransactionList : React.FC = () : JSX.Element => {

    const {transactions} : any = useContext(GlobalContext)
    return (
        <>   
            <h3>History</h3>
            <ul className="list">
                { 
                    transactions.map( (transaction : IState , index : number)  => 
                        (<Transaction key={index} transaction={transaction} />
                    )) 
                 }
            </ul>
        
        </>
    )
}

Transaction.tsx

    import React from 'react'
    import {IinitialStateType} from '../Types/expense_type'
    export const Transaction : React.FC<IinitialStateType>= (props : any) : JSX.Element => {

    let sign : string = props.transaction.amount > 0 ? '+' : '-'

    return (
        <>
            <li className={props.transaction.ammount < 0 ? "minus" : "plus"}>
                {props.transaction.text}{" "}
                <span>
                    {sign}${Math.abs(props.transaction.ammount)}
                </span>
                <button
                    className="btn-delete"
                >
                    x
                </button>
            </li>
        </>
    )
}

但是在编译时会在TransactionList.tsx文件中给出错误

TypeScript error in C:/Users/Abdul Rehman Aziz/Desktop/New folder/expense-tracker-ts/src/Components/TransactionList.tsx(14,88): Type '{ key: number; transaction: IState; }' is not assignable to type 'IntrinsicAttributes & IState & { children?: ReactNode; }'.   Property 'transaction' does not exist on type 'IntrinsicAttributes & IState & { children?: ReactNode; }'.  TS2322

    12 |             <h3>History</h3>
    13 |             <ul className="list">
  > 14 |                  { transactions.map( transaction => (<Transaction key={transaction.id} transaction={transaction} />)) }
       |                                                                                        ^
    15 |             </ul>
    16 |         </>
    17 |         </>
越南的

您必须为React.FC指定道具的类型,而不是状态。像这样:

export const Transaction: React.FC<{ transaction: IState }> = (
  props: any

我还制作了一个工作示例:https : //codesandbox.io/s/focused-sanderson-e6zlm?file=/src/App.tsx

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 React Hooks,当我将 prop 从父组件传递给子组件时,子组件中的 prop 未定义

将 prop 从父组件传递给子组件时出错

同时将组件作为prop传递时,如何在TypeScript(在React中)中强制传递prop?

将无类型字符串传递给带有类型道具的 React 组件时出现 TypeScript 错误?

我可以以编程方式将 prop 传递给 react 中的子组件吗?

当事件在父组件中触发时,将值从 React 子组件传递给父组件?

React TypeScript将函数传递给子组件

传递给子组件的 Prop 在更新父组件中的 prop 时不会改变它在子组件中的值

使用TypeScript时如何将prop传递给material-ui @ next中的自定义根组件?

将事件传递给React中的子组件

将道具传递给子组件作为React中的状态

将函数向下传递给React中的子组件

将 props 传递给 React 中的子组件

将道具传递给Typescript中的React样式的组件

将React Route传递给子组件

React:将功能传递给子组件

React 将事件传递给子组件

REACT将数组传递给子组件

在使用axios的React中尝试将多个参数传递给获取请求时出现415错误

TypeScript错误将prop传递给样式组件

如何将异步状态传递给子组件prop?

使用ReactJS通过Prop将函数传递给子组件

如何使用 React 和 Typescript 将 html 属性传递给子 <img> 元素而不会出现“IntrinsicAttributes & HTMLAttributes<HTMLImageElement> 错误”?

子视图组件将prop传递给Vue中的父布局

React-Redux存储或将prop中的api传递给组件,这是更好的方法

在Typescript中具有React组件时将React组件作为prop注入

将数组传递给 React 组件,然后在子组件中访问该数组中的值

React JS 将数据或子组件传递给父组件

将prop传递给样式化组件中的嵌套元素