The team is now working on the WordPress Interactivity API. This unblocks the same UX Frontity framework enabled but directly in WordPress Core, fully compatible with the new Site Editor.
I’m in the process of using Frontity to re-do my site in React.
My old site is a Wordpress site and I really only want to continue using it as a CMS for storing the many previously uploaded images.
I’m having trouble understanding how I would go about using the Wordpress REST API to access these images
I’ve seen some previous posts that talk about using actions.source.fetch() within a useEffect within the component, and others that do beforeSSR calls in the top most index.js file but it’s getting super confusing
It would help to know what exactly is needed here to fetch this particular image and have it displayed in my component as clear and concise as possible. Thank you!
alexis.doroszkiewicz2
There are multiple ways you can do this as you already noticed.
In most cases, your images are going to reside inside your page ‘content’ and will be accessible after getting your page’s data doing the following :
const data = state.source.get(frontity.state.router.link)
const page = state.source[data.type][data.id]
data contains information about your page, is it fetching? is it ready? is it a page? is it a post? page id? and so on.
once page data is ready, you can access it in the state using state.source[data.type][data.id]
there you will get an object with your page content, including attachment images and so on.
In your specific case however, since your Logo image is not attached to a specific page, i would recommand adding it to your state before SSR.
to do so, you could do the following (code goes into your root index.js containing theme state and actions, put this under the actions.theme namespace
// import fetch at top of file
import { fetch } from "frontity";
// this goes into actions: { theme: { ...
beforeSSR: async ({ state }) => {
// // fetch the logo
await fetch('http://www.scramblelock.com/wp-json/wp/v2/media/10325')
// convert the response to json format
.then((response) => response.json())
// add your image to the theme state
.then((image) => (state.theme.logo = image));
}
by doing so, your image should now be accessible in your site’s state
you access the frontity state by using the ‘connect’ function so your component looks like this