如何正確附加到在功能組件中作為道具傳遞的字符串數組 (How to properly append to string array that was passed as props in functional component)


問題描述

如何正確附加到在功能組件中作為道具傳遞的字符串數組 (How to properly append to string array that was passed as props in functional component)

我有一個父組件,它定義了一個字符串數組的 useState 鉤子。然後我有一個需要更新字符串數組的子組件,所以我同時傳遞了字符串數組和 useState 的函數來更新它。但是,當我嘗試引用 props 數組時,解釋器會抱怨(參見代碼/註釋):

ParentComponent.tsx:

import React, { useState } from 'react';
import { ChildComponent } from './ChildComponent';

export function ParentComponent() {
    const [log, setLog] = useState<string[]>([]);

    return(
        <ChildComponent log={log} setLog={setLog} />
    );
};

ChildComponent.tsx

import React, { useEffect } from 'react';

interface Props {
    log: string[],
    setLog: Function
};

export function ChildComponent(props: Props) {

    useEffect(() => {

        /* I typically use this pattern to append to an array in a useState hook:

        setArray(array => [...array, "String to append"]);

        so I try to use this with the props as well: */

        props.setLog(props.log => [...props.log, "Loaded child component."]); // Interpreter complains at arrow: ',' expected. ts(1005)
    },[]);

    return (
        <div>
            {props.log.map(logItem => <p key={logItem}>{logItem}</p>)}
        </div>
    );
};

我做錯了什麼?


參考解法

方法 1:

setLog: Function is not precise enough. You need:

interface Props {
    log: string[],
    setLog: React.Dispatch<React.SetStateAction<Array<string>>>
};

And this

props.setLog(props.log => [...props.log, "Loaded child component."]);

should be

props.setLog(previousLog => [...previousLog, "Loaded child component."]);

Or, in this case, it looks like it'll always have the current value of the log in the prop‑closure when this line runs, so it can be simplified to

props.setLog([...props.log, "Loaded child component."]);

(by Brian ReadingCertainPerformance)

參考文件

  1. How to properly append to string array that was passed as props in functional component (CC BY‑SA 2.5/3.0/4.0)

#use-state #react-hooks #TypeScript #reactjs






相關問題

為什麼在使用 useState 時初始加載狀態設置為 true? (Why the initial loading state is set to true while using useState?)

如何在 React 中提取狀態的值 (How to extract value of a state in React)

如何在使用狀態掛鉤更新狀態時覆蓋具有相同鍵的對象 (How can overwrite objects that have the same key in updating State with state hooks)

如何使用 useState 將許多元素添加到數組中? (How to add many elements to an array with useState?)

如何正確附加到在功能組件中作為道具傳遞的字符串數組 (How to properly append to string array that was passed as props in functional component)

useState 在功能上與組件 React 的行為不同 (useState behaves differently in function vs componenet React)

如何在反應鉤子中設置多個值來狀態 (How to set multiple values to state in react hooks)

在 reactJS 中使用 useState 和外部過濾器 (using useState and External Filters in reactJS)

React.js 更改下拉值 (React.js to change dropdown value)

useState 不會立即更新狀態 (useState is not updating state immediately)

使用 useState 時,Reactjs 不會立即更新分配的值 (Reactjs doesn't immediately update the assigned value when useState is used)

如何為 useState 創建動態函數名稱? (How do I create a dynamic function name for useState?)







留言討論