Skip to content
Rain Hu's Workspace
Go back

[IT] React + .Net

Rain Hu

React + .Net

一、環境設置 Setup

1. 行前準備 Prerequisites

node -v
npm -v
dotnet --version
import React from 'react'

const index = () => {
  return (
    <div>index</div>
  )
}

export default index

2. 創建 React

npm install create-react-app
npx create-react-app {project} --template typescript
cd {project}
yarnpkg add --exact react-dom react-scripts

3. 基本結構

  1. 函數式元件(Functional Component):
const Card: React.FC<Props> = ({
    companyName,
    ticker,
    price
}: Props): JSX.Element => { ... }
  1. JSX 標籤:
return (
    <div className='card'>...</div>
)

4. State

import React, { useState } from 'react'

type Props = {}

const Button: React.FC<Props> = (props: Props): JSX.Element => {
    const [count, setCount] = useState<number>(0);

    const onClick = (e: any) => {
        setCount(count + 1);
        console.log(e);
    }
    return (
        <div>
            <button onClick={(e) => onClick(e)}>Click me</button>
            <p>You clicked {count} times</p>
        </div>
    )
}

export default Button
  1. 引入 React 和 useState Hook:
import React, { useState } from 'react'
  1. 使用 useState Hook 定義狀態
const [count, setCount] = useState<number>(0);
  1. 定義 onClick 事件處理函數:
const onClick = (e: any) => {
    setCount(count + 1);
    console.log(e);
}

Share this post on:

Previous
[Swift] UI Challenge
Next
[IT] ApiController Atrribute