Getting started with React and TypeScript - Startxlabs

Getting started with React and TypeScript

What is TypeScript?

TypeScript is a typed superset of JavaScript, that makes the React components more resilient without complex building and coding. It works on all browsers and OS and assists in reducing compile-time errors. All the features of JavaScript are available in TypeScript as TypeScript is a superset of it. Some of the most important features of TyoeScript which aid in efficient development include code refactoring, type checking, navigation features and so on.

Features that TypeScript adds to ReactJs

There are many advanced features added to ReactJs, that enable easy and efficient development in React. Some of the important features added by TypeScript are,

Why use TypeScript with React?

You may even ask, “why should I use TypeScript in React when JS is available? What are all the benefits I can get?”. Let’s go for the answers to this question now.

Read our article on – Why is React Native the best for Mobile App Development?

How to get started using TypeScript?

You can start using TypeScript by using the Create React App developed by Facebook by importing the create-react-app npm package. The CRA will provide many built-in features, Webpack, hot reloading development that assist you during the entire development process.

To ensure type-checked tests don’t make a mismatch between the development editor and CRA’s output, another npm script known as “test:tsc”: “tsc -p tsconfig.test.json -w” should be used. After the completion of type-checking tests, you can now successfully start creating your app using React using TypeScript.

There are two types using which you can build your application using TypeScript. One is using the Parcel, another is the Webpack. You can use TypeScript with classes, functional components, and also with props.

Functional Component

import * as React from 'react';

const Count: React.FunctionComponent<{
  count: number;
}> = (props) => {
  return <h1>{props.count}</h1>;
};

export default Count;

This function is written to define object the structure of the props. It can also be done using the following:

interface Props {
  count: number;
}

const Count: React.FunctionComponent<Props> = (props) => {
  return <h1>{props.count}</h1>;
};

Read our article on – Custom Tab Bar in React Native using SVG, and D3-Shape

Class Component

Class components can be created by using TypeScript like –

import * as React from 'react';

import Count from './Count';

interface Props {}

interface State {
  count: number;
};

export default class Counter extends React.Component<Props, State> {
  state: State = {
    count: 0
  };

  increment = () => {
    this.setState({
      count: (this.state.count + 1)
    });
  };

  decrement = () => {
    this.setState({
      count: (this.state.count - 1)
    });
  };

  render () {
    return (
      <div>
        <Count count={this.state.count} />
        <button onClick={this.increment}>Increment</button>
        <button onClick={this.decrement}>Decrement</button>
      </div>
    );
  }
}

Default Props

By using default props, we can develop our code in TypeScript like –

import * as React from 'react';

interface Props {
  count?: number;
}

export default class Count extends React.Component<Props> {
  static defaultProps: Props = {
    count: 10
  };

  render () {
    return <h1>{this.props.count}</h1>;
  }
}

render () {
  return (
    <div>
      <Count />
      <button onClick={this.increment}>Increment</button>
      <button onClick={this.decrement}>Decrement</button>
    </div>
  )
}

“We transform your idea into reality, reach out to us to discuss it.

Or wanna join our cool team email us at [email protected] or see careers at Startxlabs.”

 

Author: Akash Upadhyay

Share this blog