简体   繁体   English

使用 useEffect 努力调试 Expo 上的无效挂钩调用

[英]Struggling to debug invalid hook call on Expo with useEffect

When selecting an address from Places Autocomplete (ie onPress), I am calling a function to try and return the value of geocodeAsync from Expo location.当从 Places Autocomplete(即 onPress)中选择一个地址时,我正在调用 function 以尝试从 Expo 位置返回 geocodeAsync 的值。 I am however getting an invalid hook call when this function is being called, any advice would be lovely.然而,当这个 function 被调用时,我收到了一个无效的钩子调用,任何建议都会很好。

function getArray(data) {
    React.useEffect(() => {
        async function getPromise() {
            const res = await geocodeAsync(data); // type: Promise<Interface>
            setCoordinates(res);
        }
        void getPromise();
    }, [])
    const [coordinates2, setCoordinates] = React.useState<LocationGeocodedLocation[]>()
    return coordinates2

}

Your outer function needs to have the prefix use so it can be parsed as a hook by React (ie useGetArray ).您的外部 function 需要use前缀,以便 React 可以将其解析为钩子(即useGetArray )。 You should also define your state before your useEffect so there's no risk of your set function being called before it's defined.您还应该在 useEffect 之前定义您的 state,这样就没有在定义之前调用您的set function 的风险。 See https://reactjs.org/docs/hooks-custom.html .请参阅https://reactjs.org/docs/hooks-custom.html

Also, consider turning on the React lint rules so you aren't missing dependencies in your useEffect.此外,请考虑启用 React lint 规则,这样您就不会在 useEffect 中丢失依赖项。

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

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