The main logic of the component is that when the page is loaded, need to save the parameters from the URL (token) and pass them from the dispatch() function. To do this, I have a state value were initially null is stored, but after rendering the component I add a token.
component.js
const [state, dispatch] = useReducer()
const [value, setValue] = useState(null)
useEffect(() => {
const query = window.location.search || ''
const queryString = new URLSearchParams(query || '')
const token = queryString.get('token')
setValue(token)
}, [value])
useEffect(() => {
if (value && value !== null) {
// console.log(1)
dispatch({
type: 'TEST_TYPE',
payload: { data: value },
})
}
}, [value])
When I want to call the dispatch() function in useEffect() the component is reloaded until the memory is full. I tested the second method, called the dispatch() function into useCallback() to call once if there are new changes. But the dispatch function is not called
const dis = useCallback(() => dispatch, [])
useEffect(() => {
const query = window.location.search || ''
const queryString = new URLSearchParams(query || '')
const token = queryString.get('token')
if (token) {
dis({
type: 'TEST_TYPE',
payload: { data: token },
})
}
}, [dis])
How can I properly make dispatch work once when the component is loaded and without an infinite loop?