Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

311
Views
Redux toolkit axios api no se llama

¿Alguien puede ayudarme a encontrar el problema exacto con el siguiente código, ya que soy nuevo en redux?

  • API no se llama
  • los datos no llegan a la matriz de productos estatales []

producto-slice.js

 import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";<br/> import axios from "axios";<br/> const initialState = {<br/> products: [],<br/> sample: "Hello World",<br/> };<br/> <br/> const productSlice = createSlice({<br/> name: "productSlice",<br/> initialState,<br/> reducers: {<br/> getProducts(state, action) {<br/> const p = action.payload;<br/> console.log(p);<br/> },<br/> },<br/> extraReducers: (builder) => {<br/> builder.addCase(fetchProductData.fulfilled, (state, action) => {<br/> state.products.push(action.payload);<br/> });<br/> },<br/> });<br/> export const fetchProductData = createAsyncThunk(<br/> "products/fetchProducts",<br/> async (_, thunkAPI) => {<br/> try {<br/> const response = await axios.get("http://localhost:8080/products");<br/> return await response.data;<br/> } catch (error) {<br/> return error;<br/> }<br/> }<br/> );<br/> export const productActions = productSlice.actions;<br/> export default productSlice;<br/> ProductList.js(Component)<br/> const ProductList = (props) => {<br/> const history = useHistory();<br/> const hello = useSelector((state) => state.products.sample);<br/> const dispatch = useDispatch();<br/> <br/> useEffect(() => {<br/> dispatch(fetchProductData);<br/> }, [dispatch]);<br/>
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Debe llamar a una action como una función dentro de dispatch() como dispatch(fetchProductData()) e implementar redux/toolkit con thunk middleware como a continuación (ejemplo):

como se puede ver en reduxToolkitCreateAsyncThunk

 import { createAsyncThunk, createSlice } from '@reduxjs/toolkit' import { userAPI } from './userAPI' // First, create the thunk const fetchUserById = createAsyncThunk( 'users/fetchByIdStatus', async (userId, thunkAPI) => { const response = await userAPI.fetchById(userId) return response.data } ) // Then, handle actions in your reducers: const usersSlice = createSlice({ name: 'users', initialState: { entities: [], loading: 'idle' }, reducers: { // standard reducer logic, with auto-generated action types per reducer }, extraReducers: (builder) => { // Add reducers for additional action types here, and handle loading state as needed builder.addCase(fetchUserById.fulfilled, (state, action) => { // Add user to the state array state.entities.push(action.payload) }) }, }) // Later, dispatch the thunk as needed in the app dispatch(fetchUserById(123))
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!