Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
128
rated 0 times [  134] [ 6]  / answers: 1 / hits: 9056  / 4 Years ago, wed, january 15, 2020, 12:00:00

I am getting the warning about React has detected a change in the order of Hooks called by Checkout



I did read https://reactjs.org/docs/hooks-rules.html and looks like my code meets the requirements



The warning:
screenshot from debbuger



Fragment of file with hooks:



  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)
}
})
}, [])


Full component code:
https://codesandbox.io/s/modest-lewin-4x9ss


More From » reactjs

 Answers
35

If you have hooks in your component, you must ensure that all hooks called on every render and in the same order. This code should be placed after all hooks (because of return):



if (!token) {
navigate.push(routes.login)
return <Text>Redirect</Text>
}

[#5050] Saturday, January 11, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tierney

Total Points: 45
Total Questions: 101
Total Answers: 94

Location: Sudan
Member since Thu, May 7, 2020
4 Years ago
;