Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

191
Vistas
Quiero usar el regreso de axios al estado global, pero Promise { <pending> }
const Board = () => { ... const {selected} = useSelector(state => state.board); // In Redux reducer -> // const initialState = { // selected : {} // } const getOnePost = async (id) => { try { const response = await axios.get(`/api/v1/post/${id}`); const getOnePostData = await response.data; selected = getOnePostData //I want to use Redux to use global state... console.log(selected) //TypeError... and How to use global state..? } catch(error) { alert(error.response.data.message); return Promise.reject(error) } } const postClickHandler = (postId) => { if(postId) { // dispatch(boardSelected(postId)); getOnePost(postId) } } ... }

Este código usa axios.get para recibir información de la publicación.

y quiero usar el retorno de api al estado global (estado Redux).

 const getOnePost = async (id) => { try { const response = await axios.get(`/api/v1/post/${id}`); const getOnePostData = await response.data; return getOnePostData //return data } catch(error) { alert(error.response.data.message); return Promise.reject(error) } } const postClickHandler = (postId) => { if(postId) { getOnePost(postId).then((response)=>{ return{...selected, selected: response} //But Seleted: {} }) } }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Axios es una biblioteca de JavaScript basada en Promised que se usa para enviar solicitudes HTTP, debe obtener el resultado de la promesa en el método then. prueba esto

 const Board = () => { ... const {selected} = useSelector(state => state.board); // In Redux reducer -> // const initialState = { // selected : {} // } const getOnePost = async (id) => { try { const response = await axios.get(`/api/v1/post/${id}`); response.then(function (rec) { selected = rec.data // //here update you state console.log(selected) }).catch(function (error) { console.log(error); }); } catch(error) { alert(error.response.data.message); return Promise.reject(error) } } const postClickHandler = (postId) => { if(postId) { // dispatch(boardSelected(postId)); getOnePost(postId) } } ...

}

about 4 years ago · Juan Pablo Isaza Denunciar

0

Puedes manejar la promesa usando .then()

Intente inicializar seleccionado como a continuación:

 const response = await axios.get(`/api/v1/post/${id}`).then( response => { selected = response })

No sé qué tipo de datos son. Por lo tanto, intente registrar los datos de respuesta y utilícelos.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda