zoukankan      html  css  js  c++  java
  • xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

    TypeScript with React

    # Make a new directory
    $ mkdir react-typescript
    
    # Change to this directory within the terminal
    $ cd react-typescript
    
    # Initialise a new npm project with defaults
    $ npm init -y
    
    # Install React dependencies
    $ npm install react react-dom
    
    # Make index.html and App.tsx in src folder
    $ mkdir src
    $ cd src
    $ touch index.html
    $ touch App.tsx
    
    # Open the directory in your favorite editor
    $ code .
    
    
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8" />
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <title>React + TypeScript</title>
      <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
      <div id="main"></div>
      <script src="./App.tsx"></script>
    </body>
    </html>
    
    # Install Parcel to our DevDependencies
    $ npm i parcel-bundler -D
    
    # Install TypeScript
    $ npm i typescript -D
    
    # Install types for React and ReactDOM
    $ npm i -D @types/react @types/react-dom
    
    
    {
      "name": "react-typescript",
      "version": "1.0.0",
      "description": "An example of how to use React and TypeScript with Parcel",
      "scripts": {
        "dev": "parcel src/index.html"
      },
      "keywords": [],
      "author": "Paul Halliday",
      "license": "MIT"
    }
    
    
    
    import * as React from 'react';
    
    export default class Counter extends React.Component {
      state = {
        count: 0
      };
    
      increment = () => {
        this.setState({
          count: (this.state.count + 1)
        });
      };
    
      decrement = () => {
        this.setState({
          count: (this.state.count - 1)
        });
      };
    
      render () {
        return (
          <div>
            <h1>{this.state.count}</h1>
            <button onClick={this.increment}>Increment</button>
            <button onClick={this.decrement}>Decrement</button>
          </div>
        );
      }
    }
    
    
    
    import * as React from 'react';
    import { render } from 'react-dom';
    
    import Counter from './Counter';
    
    render(<Counter />, document.getElementById('main'));
    
    
    $ npm run dev
    # open http://localhost:1234.
    
    

    Create React App and TypeScript

    $ create-react-app my-new-app --typescript
    

    https://www.digitalocean.com/community/tutorials/react-create-react-app

    https://facebook.github.io/create-react-app/docs/adding-typescript

    Functional Components

    https://www.digitalocean.com/community/tutorials/react-functional-components

    import * as React from 'react';
    
    const Count: React.FunctionComponent<{
      count: number;
    }> = (props) => {
      return <h1>{props.count}</h1>;
    };
    
    export default Count;
    
    
    interface Props {
      count: number;
    }
    
    const Count: React.FunctionComponent<Props> = (props) => {
      return <h1>{props.count}</h1>;
    };
    
    

    Class Components

    
    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

    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>
      )
    }
    
    

    refs

    https://www.digitalocean.com/community/tutorials/react-typescript-with-react



    ©xgqfrms 2012-2020

    www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


  • 相关阅读:
    gflag使用
    INTERVIEW #2
    Decision Tree
    Java FAQ
    K-Nearest Neighbors
    INTERVIEW #1
    C++ FAQ
    INTERVIEW #0
    Selection Sort
    TCP 3-Way Handshake
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/13549852.html
Copyright © 2011-2022 走看看