2

I am trying to pass value to component props.
This is what I do:

I create interface:

interface CheckoutItem {
    id: string,
    name: string,
    img: string,
    unitPrice: number,
    amount: number,
}    

I create display component:

const CheckoutDisplay = (items: CheckoutItem[] ) => {
    return (
        <div>
            {items.map(item => (
                <p>
                    {item.name}
                </p>
            ))}
        </div>
    )
}    

I create container component:

const Checkout = (items: CheckoutItem[], setAmount: any) => {
  return <CheckoutDisplay items={items} setAmount={setAmount} />;    
  // error here:    
  // Type '{ items: CheckoutItem[]; setAmount: any; }' is not assignable to type 
  // 'IntrinsicAttributes & CheckoutItem[]'.
  //  Property 'items' does not exist on type 'IntrinsicAttributes & CheckoutItem[]'.
};

const mapStateToProps = (state: any) => {
  return {
    items: state.items,
  };
};

const mapDispatchToProps = (dispatch: AppDispatch) => {
    return {
        setAmount: (id: number, amount: number) => dispatch(setItemAmount(id, amount)),
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(Checkout);    

What is wrong with my code?
Thank you!

1 Answer 1

3

Props are passed as an object to CheckoutDisplay, so you need to destructure them.

interface CheckoutDisplayProps {
  items: CheckoutItem[],
  setItemAmount: any,
}

const CheckoutDisplay: React.FunctionComponent<CheckoutDisplayProps> = ({ items, setItemAmount }) => {
    return (
        <div>
            {items.map(item => (
                <p>
                    {item.name}
                </p>
            ))}
        </div>
    )
}  
Sign up to request clarification or add additional context in comments.

2 Comments

I think the best practice use const CheckoutDisplay: React.FC<CheckoutDisplayProps>
No, deprecated React.SFC and React.StatelessComponentgithub.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.