【发布时间】:2020-01-15 11:26:17
【问题描述】:
我收到关于 React has detected a change in the order of Hooks called by Checkout 的警告
我确实阅读了https://reactjs.org/docs/hooks-rules.html 并且看起来我的代码符合要求
带有钩子的文件片段:
if (!token) {
navigate.push(routes.login)
return <Text>Redirect</Text>
}
const maximumDate = moment().add(1, 'year')
const minimumDate = moment()
const formattedToday = minimumDate.format('YYYY-MM-DD')
const [paymentMethod, setPaymentMethod] = useState(paymentOptions[0].key)
const [totalAmount, setTotalAmount] = useState(totalCartAmount)
const [deliveryTime, setDeliveryTime] = useState(0)
const [date, setDate] = useState(minimumDate)
const [show, setShow] = useState(false)
const validationSchema = yup.object().shape({
couponCode: yup.string(),
comments: yup.string(),
})
useEffect(() => {
if (deliverySlots.length > 0) {
setDeliveryTime(deliverySlots[0].id)
}
}, [deliverySlots])
useEffect(() => {
getDeliveryTimeSlots(country.id, formattedToday, error => {
if (error) {
console.log(error)
}
})
}, [])
【问题讨论】:
-
你在一个条件之后执行钩子(即
if (!token))
标签: javascript reactjs react-native react-hooks