How do I use createAsyncThunk to repeatedly fetch data automatically?

Viewed 28

The goal is to repeatedly fetch generation data when the Id changes...The generation Id changes at the backend

Only AFTER I used redux tool kit and createAsyncThunk, did it stop refreshing generation Id automatically, unless I manually refresh the page (will it show the latest generationId)

The change I've made, so how should I do it? Thanks.

in generationSlice.js

export const getGeneration = createAsyncThunk(
  'generation/getGeneration',
  async () => {
    try {
      const resp = await axios(url)
      return resp.data
    } catch (error) {
      return error.response
    }
  }
)

and

in components/Generation.js

const { generationId, expiration, isLoading } = useSelector((store) => {
  return store.generation
})

const dispatch = useDispatch()

useEffect(() => {
  dispatch(getGeneration())
}, [generationId])

0 Answers
Related