concise and readable syntax for props in a react component in typescript

tatsu

So if you declare a React.FC , then you get to do a type declaration and thus get to pass it props :

const FuntionalComponent: React.FC<personType> = ({ id, nationality, name, familyName, age, selected }) =>

    <div>
       ...directly html
    </div>

export default FuntionalComponent;

But you cannot declare any methods or use hooks there (I have not found a way)

Then there's the React.Component type :

class Component extends React.Component<{}, {Stateproperty: string}>{

  constructor(){
  }

  hook(){
  }

  method(){
  }

  render() {
    return (
      <div>
         ...html finally
      </div>
    )
  }
}

export default component;

as you can see I can pass a state but not a props.

If I try something like this :

class Component extends React.Component<{propsProperty: Array}, {Stateproperty: string}>{

and then add my propsProperty to my html :

<Component propsProperty={thisArray} />

Yet, they error out with the following entry:

TS2322: Type '{ propsProperty: any; }' is not assignable to type 'IntrinsicAttributes & { children?: ReactNode; }'.   Property 'tankData' does not exist on type 'IntrinsicAttributes & { children?: ReactNode; }'.

These tutorials seem to indicate that there is no other way to declare components:

https://riptutorial.com/reactjs/example/25321/declare-default-props-and-proptypes https://medium.com/@cristi.nord/props-and-how-to-pass-props-to-components-in-react-part-1-b4c257381654

I found this article about TypeScript errors in React: https://medium.com/innovation-and-technology/deciphering-typescripts-react-errors-8704cc9ef402, but it didn't have my issue.

I also tried this solution : https://stackoverflow.com/a/47395464/4770754. Even though it's clearly not the same issue, it seemed somewhat close, yet it didn't help.

React docs are not helpful since they ignore TypeScript and are not human-readable.

I need a way that's both concise, respects TypeScript and allows for both props and methods within the class body. Does this not exist at all?

gqstav

But you cannot declare any methods or use hooks there (I have not found a way)

A good standard way of declaring a FC is:

type ComponentProps = {
  id: string, 
  nationality: string, 
  ...
}

const MyComponent: React.FC<ComponentProps> = ({
  id, 
  nationality, 
  ...rest
  }: ComponentProps) => {
  
  const someMethod = () => {
    console.log('I am console logging');
  }


  return(
    <div>
      {/* HERE YOU WILL RENDER STUFF */}
    </div>
  )
}

Note that in the above I deconstruct the props on instantiation so that id, nationality can be leveraged directly in the component.

I don't think you need to worry too much about the syntax highlighting until you're familiar with the above.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Organizing React Component Props in TypeScript

IntrinsicAttributes props in React Typescript component

React & Typescript component props type for `Component`

Reactjs, Typescript - Casting props in typescript - React Component

React + Typescript: Type of React component with no state/no props

Typescript React Functional Component With Multiple Props Type

Send onClick function as props to a component in React with Typescript

React and Typescript: How to extend generic component props?

TypeScript React, unable to send props to component

React & TypeScript usage of component passed via props

typecast react typescript component class props

React + Typescript object props to component error

Pass component props in Private Route with Typescript and React

TypeScript error in passing props to child component in react

Typescript: Defining undeclared props for generic React component

React typescript pass props interface to the class component

Passing props from class component in react + typescript

React + TypeScript Syntax: Dynamically change container component

Syntax for Typescript prop types for React component?

React Typescript Functional Component Syntax Error

TypeScript conditional types: Extract component props type from react component

How to pass the props to react functional component using react table and typescript?

How to access props in react functional component using react and typescript?

React typescript with react-redux component requires all passed props

Extract the type of the component props in react-storybook with typescript

Getting toogleShow is not function when passing as props in react typescript functional component

Typescript generic used in React component props requires arguments error

Default property value in React component with extended props using TypeScript 3

Rendering react component using typescript without passing in strongly typed props