运行过了,并没有执行。你看我敲的代码:
import { useEffect, useState } from "react"
import { SafeAreaView, TouchableOpacity, Text } from "react-native";
export default ( props ) => {
const [ msg, setMsg ] = useState('');
useEffect(() => {
console.log('useEffect')
// 对应componentDidUpdate
function handleStatusChange( status ) {
console.log('handleStatusChange', status );
}
const timer = setTimeout( () => {
doFetch();
}, 2000)
// handleStatusChange();
// 对应componentWillUnmount
return function cleanup() {
timer && clearTimeout(timer);
}
})
const doFetch = () => {
setTimeout( () => {
setMsg('返回结果为:123')
}, 1000)
}
return (
加载
{ msg }
)
}
调用打印结果为:
useEffect
useEffect
这个handleStatusChange是useEffect钩子函数还是自定义的函数?是否需要在某个地方手动调用?为何说它与componentDidUpdate对应?没搞太懂,望老师能详细跟我说说,3Q