I’m trying to fetch data from the api and returns a promise does anyone know how to handle a response in frontity?
Hi @hugsaf2132
Could you provide a bit more info about your issue. Please see this post about the kind of information to include so that people can better help you: About the 🤗 Dev Talk & Questions category
Hi @hugsaf2132
Here you have a few guide of what promises are and how to work with them:
- https://codeburst.io/a-simple-guide-to-es6-promises-d71bacd2e13a
- https://alligator.io/js/promises-es6/
- https://web.dev/promises/
Hope this helps
158/5000
Good afternoon, even following the promises I have some difficulty mainly in frontity. someone who could help me, I already left the link to my project
HI @hugsaf2132
Can you specify the problem you are having for fetching data ?
In the docs you have and example of how to use fetch
in a React Component and an explanation of each step
https://docs.frontity.org/api-reference-1/wordpress-source#how-to-use
import React, { useEffect } from "react";
import { connect } from "frontity";
// In a React component that uses "connect":
const CategoryNature = ({ state, actions }) => {
// 1. fetch data related to a path
// With this useEffect we make the call to fetch
// only the first time the component is rendered.
// When the data is fetched, the state is updated with the new data
// so the component is re-rendered and "data" will get proper content
useEffect(() => {
actions.source.fetch("/category/nature/");
}, []);
// 2. get data from frontity state
const data = state.source.get("/category/nature/");
// 3. get entities from frontity state
if (data.isCategory) {
// the category entity
const category = state.source.category[data.id];
// posts from that category
const posts = data.items.map(({ type, id }) => state.source[type][id]);
// 4. render!
return (
<>
<h1>{category.name}</h1>
{posts.map((p) => (
<a href={p.link}>{p.title.rendered}</a>
))}
</>
);
}
return null;
};
export default connect(CategoryNature);