Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript & React: Passing props between components vs default props

I am fairly new to Typescript and creating a React app with Typescript. I'm having a bit of trouble passing props from one component to another. I've included an example below, my issue is around default props for components.

When I call my child component in my parent component, I get an error:

Type '{}' is missing the following properties from type 'IProps': className, disabled ts(2739)

I thought that because I have default props on my child component, they would fill in for any missing props when calling the component from other components.

I know I can make individual props optional in the interface IProps in my child component using className?: string but this is not a solution I'm looking for as it presents more problems than it solves.

I'd prefer not to have to note each default prop when I call a child from another component like below as for some components, I have many props: <Child class={''} disabled={false} />

I'm sure there's a fairly simple solution for this but I can't find any direction so far. Any advice or direction would be welcome.

// Parent component: 

import React, { FC } from 'react'

import Child from './child'

const Parent: FC = () => {
    return (
        <Child />
    )
}

export default Parent


// Child component: 

import React, { FC } from 'react'

interface IProps {
  className: string
  disabled: boolean
}

const Child: FC<IProps> = ({ className, disabled }: IProps) => {
  return (
    <button className={className} disabled={disabled}>
      Click here
    </button>
  )
}

Child.defaultProps = {
  className: '',
  disabled: false,
}

export default Child
like image 478
paulosullivan22 Avatar asked Sep 25 '19 07:09

paulosullivan22


People also ask

What is TypeScript used for?

TypeScript is a superset of typed JavaScript (optional) that can help build and manage large-scale JavaScript projects. It can be considered JavaScript with additional features like strong static typing, compilation, and object-oriented programming.

Is TypeScript better than JavaScript?

Advantages of using TypeScript over JavaScriptTypeScript always points out the compilation errors at the time of development (pre-compilation). Because of this getting runtime errors is less likely, whereas JavaScript is an interpreted language. TypeScript supports static/strong typing.

Is TypeScript frontend or backend?

Is TypeScript Frontend or Backend? TypeScript is neither a frontend or backend language, but rather a superset of the already established and well-known software language, JavaScript.

Should I learn JavaScript or TypeScript?

We frequently see the question “Should I learn JavaScript or TypeScript? “. The answer is that you can't learn TypeScript without learning JavaScript! TypeScript shares syntax and runtime behavior with JavaScript, so anything you learn about JavaScript is helping you learn TypeScript at the same time.


Video Answer


1 Answers

Solved it, for anyone looking at this answer: just need to pass in the default props into the component as well as any props as per code below:

import React, { FC } from 'react'

interface IProps {
  className: string
  disabled: boolean
}

const Child: FC<IProps & Child.defaultProps> = ({ className, disabled }: IProps) => {
  return (
    <button className={className} disabled={disabled}>
      Click here
    </button>
  )
}

Child.defaultProps = {
  className: '',
  disabled: false,
}
like image 50
paulosullivan22 Avatar answered Sep 30 '22 15:09

paulosullivan22