【发布时间】:2020-09-15 19:50:47
【问题描述】:
我是 hooks 的新手,在 SO 上遇到了这个设置,想确认这是正确的模式。我之前收到了 RN“未安装的组件”泄漏警告消息,这似乎解决了它。我试图以某种方式模仿compnentDidMount。这是电话号码验证注册流程和 onMount 的一部分,我只想检查 navigation,然后触发副作用,设置挂载 true,然后正确卸载。
const SMSVerifyEnterPinScreen = ({ route, navigation }) => {
const [didMount, setDidMount] = useState(false)
const { phoneNumber } = route.params
useEffect(() => {
if(navigation) {
signInWithPhoneNumber(phoneNumber)
setDidMount(true)
}
return () => setDidMount(false)
}, [])
if (!didMount) { return null }
async function signInWithPhoneNumber(phoneNumber) {
const confirmation = await auth().signInWithPhoneNumber('+1'+phoneNumber)
...
}
return (
...
)
}
带有 react-nav 5 的 RN 0.62.2 - 谢谢!
【问题讨论】:
-
didMount的目的是什么。它似乎没有完成任何事情。你在signInWithPhoneNumber中设置一些状态吗? -
是在
signInWithPhoneNumber中设置多个状态
标签: react-native react-hooks react-functional-component