技術筆記 / JavaScript / TypeScript

React 基本小筆記

指令

創建 react typescript project

 npx create-react-app my-app --template typescript

vs code config

Run Debug

* 在 console ``` npm start ```
* F5 play!!!

## JSX

### jsx function example
```typescript!
const TestRow = (props: any): JSX.Element => (
  <div>
    xxxx {props.name}
    <button onClick={testEvent}> oooo </button>
    <img src={require('./download.png')}></img>
    <img src={logo} width={50} height = {50} ></img>
  </div>)

```typescript! const FragmentTest = (props: any): JSX.Element => { console.log(props); return

  • dd
  • cc
  • vv
  • }

    
    ### list 渲染 - Array
    ```typescript!
      const vals = [
        <li key={1}> s- </li>,
        <li key={2}> b* </li>,
        <li key={3}> x1 </li>,
      ];
      return <>
        {vals}
      </>
    

    !important 記得要加 key

    props 常用

    props.children

    取得標籤內部的 jsx

    ex : 取得內部的 jsx 的 attribute 屬性 ```typescript! console.log(props.children[0].props.testTag);

    
    ## hook
    
    ### useEffect use await example
    
    ```typescript=
    import React, { useEffect } from 'react';
    
    function MyComponent() {
      useEffect(() => {
        // Define an async function inside the useEffect
        const fetchData = async () => {
            const response = await fetch('https://api.example.com/data');
            const data = await response.json();
        };
        // Call the async function
        fetchData();
      }, []); // Dependencies array, ensure to add variables that might affect the fetch operation
    
      return <div>Check the console for the fetched data.</div>;
    }
    
    export default MyComponent;
    
    
    tags: React TypeScript

    在 GitHub 看原文