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

165
Views
reaccionar | Estado compartido de Redux Toolkit Indefinido

Estoy creando una aplicación simple para jugar con Redux ToolKit y reaccionar; sin embargo, puedo ver el objeto en la pestaña cromada redux, pero no puedo acceder a él a través de componentes que usan ganchos de reacción.

mi rebanada:

 import { createSlice, createSelector, createAsyncThunk, } from "@reduxjs/toolkit"; const initialState = { cryptoList: [], loading: false, hasErrors: false, }; export const getCryptos = createAsyncThunk("cryptos/get", async (thunkAPI) => { try { const cryptosUrl = "https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd"; let response = await fetch(cryptosUrl); console.log(response); return await response.json(); } catch (error) { console.log(error); } }); const cryptoSlice = createSlice({ name: "cryptos", initialState, reducers: {}, extraReducers: (builder) => { builder.addCase(getCryptos.pending, (state) => { state.loading = true; }); builder.addCase(getCryptos.fulfilled, (state, { payload }) => { state.loading = false; state.cryptoList = payload; }); builder.addCase(getCryptos.rejected, (state, action) => { state.loading = false; state.hasErrors = action.error.message; }); }, }); export const selectCryptos = createSelector( (state) => ({ cryptoList: state.cryptoList, loading: state.loading, }), (state) => state ); export default cryptoSlice;

Mi componente:

 import React, { useEffect } from "react"; import { getCryptos, selectCryptos } from "./cryptoSlice"; import { useSelector, useDispatch } from "react-redux"; const CryptoComponent = () => { const dispatch = useDispatch(); const { cryptoList, loading, hasErrors } = useSelector(selectCryptos); useEffect(() => { dispatch(getCryptos()); }, [dispatch]); const renderCrypto = () => { if (loading) return <p>Loading Crypto...</p>; if (hasErrors) return <p>Error loading news...</p>; if (cryptoList) { return cryptoList.data.map((crypto) => <p> {crypto.id}</p>); } }; return ( <div className="container"> <div className="row">CryptoComponent: {renderCrypto()}</div> </div> ); }; export default CryptoComponent;

Todos los valores construidos del estado: cryptoList, loading, hasErrors, parecen no estar definidos a nivel de componente.

¡Cualquier sugerencia es apreciada!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

¿Has intentado usar el siguiente código createSelector:

 export const selectCryptos = createSelector( (state) => state, (state) => ({ cryptoList: state.cryptoList, loading: state.loading, }) );

Según el estado de la documentación, debe ser el primer parámetro: https://redux.js.org/usage/deriving-data-selectors

about 4 years ago · Juan Pablo Isaza 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!