简体   繁体   English

React 功能组件:TypeError: handleFlagChange is not a function

[英]React functional component: TypeError: handleFlagChange is not a function

Consider the following:考虑以下:

I have a parent functional component with a nested child component,我有一个带有嵌套子组件的父功能组件,

function Parent(){
  const [isFlagTrue, setIsFlagIsTrue] = useState(false);

    const handleFlagChange = (bool) => setIsFlagIsTrue(bool)

    useEffect(() => {

    console.log("isFlagTrue ", isFlagTrue);
 }, [isFlagTrue])

  return (
    <Child handleFlagChange={handleFlagChange} />
  )
}

In the Child I'm making an async call to populate a data-table;在 Child 中,我正在进行异步调用以填充数据表;

import { useDispatch } from 'react-redux';


function Child({handleFlagChange}) {

    const dispatch = useDispatch();
    const componentIsMounted = useRef(true)
    const [rows, setRows] = useState([]);

    useEffect(() => {

        if (currentProductItem && currentProductItem.value != null) {
            
               dispatch(getClientVariables(currentProductItem.value)).then(r => {
                rawData.current.Rows = r.payload;
                dispatch(getClientVariableTypesAll(currentProductItem.value)).then(r => {

                    rawData.current.ClientDataVariableTypes = r.payload.map(r => {
                        return {
                            value: r.ClientDataVariableTypeID,
                            label: r.ClientDataVariableTypeName
                        }
                    });;
                    setRows(rawData.current.Rows);
                    console.log('rawData', rawData);

                });
            });
        }
    }, [currentProductItem, justSavedNewVariableTypes, justSavedGrid]);

}

    useEffect(() => {
    console.log("typeof handleFlagChange ", typeof handleFlagChange);

    console.log("rows ", rows);

    // if (componentIsMounted.current) {
    //  var flag = rows.some(item => item.ClientDataVariableTypeName == null)
    //  handleFlagChange(flag)
    // }

    if (Array.isArray(rows) && typeof handleFlagChange != 'undefined') {
        console.log("foo ");
        var flag = rows.some(item => item.ClientDataVariableTypeName == null)
        handleFlagChange(flag)
    }
}, []);

useEffect(() => {
    return () => {
        componentIsMounted.current = false
    }
},[])
    
   ....other code & rendering    
    
}

I am expecting the isFlagTrue console in the useEffect of the parent to fire when the rows have been validated in the child by the return value of the some function on the array of rows.当行已通过行数组上的some function 的返回值在子级中验证行时,我期望父级的 useEffect 中的isFlagTrue控制台触发。

I have tried two solutions one is insuring the <Child/> is mounted (and having the data call being full-filled) by setting a ref to true using useRef() .我已经尝试了两种解决方案,一种是通过使用useRef()将 ref 设置为 true 来确保已安装<Child/> (并使数据调用已满)。

In Child: const componentIsMounted = useRef(true)在子级中: const componentIsMounted = useRef(true)

useEffect(() => {
    console.log("typeof handleFlagChange ", typeof handleFlagChange);


     if (componentIsMounted.current) {
       var flag = rows.some(item => item.ClientDataVariableTypeName == null)
       handleFlagChange(flag)
      }

}, []);

useEffect(() => {
    return () => {
        componentIsMounted.current = false
    }
},[])

But I get TypeError: handleFlagChange is not a function但我得到TypeError: handleFlagChange is not a function

So then I tried:所以我尝试了:

useEffect(() => {
        console.log("typeof handleFlagChange ", typeof handleFlagChange);

        if (componentIsMounted.current && typeof handleFlagChange != 'undefined' && typeof handleFlagChange != 'undefined') {
            console.log("foo ");
            var flag = rows.some(item => item.ClientDataVariableTypeName == null)
            handleFlagChange(flag)
        }
    }, []);

But that yields:但这会产生:

typeof handleFlagChange  undefined. <---In the Child
isFlagTrue  false <--- In Parent

Any ideas?有任何想法吗?

Have you consider using a default value for the function and let the normal flow of React do yours?您是否考虑过为 function 使用默认值并让 React 的正常流程为您服务?

function Child({handleFlagChange = () => {}})

Let me know if this works.让我知道这个是否奏效。

I think this is a typescript error saying that the type of handleFlagChange is not known in child Component.我认为这是一个 typescript 错误,表示子组件中不知道 handleFlagChange 的类型。

You have 2 option here:您在这里有 2 个选项:

First, declare type of each prop at first and then use it in function:首先声明每个prop的类型,然后在function中使用:

 type PropsType = { handleFlagChange: () => void }
    
 function Child({handleFlagChange}: PropsType)

Or try to add specific type in the function itself:或者尝试在 function 本身中添加特定类型:

function Child({handleFlagChange: () => void})

where () => void being the type of handleFlagChange function.其中() => void是 handleFlagChange function 的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM