Some developers just prefer working in React. I don’t blame them really, because I like React too. Maybe that’s what they learned first. I’ve been using it long enough there is just some comfort to it. But mostly it is the strong component model that I like. There is just something nice about a codebase where things are constructed from components with clear jobs and responsibilities.
It’s not terribly common to see WordPress sites built with React though. The standard way to use WordPress is through themes that are essentially styles and PHP files that handle the templating. Frontity is changing that though. Frontity is a React-powered framework that digests your WordPress site’s API and builds the entire front end in React with all the powerful tools you’ve come to expect from that type of environment.
OMG, Now That’s a Fast Setup
This is how I was able to get started. At the command line, I did:
npx frontity create my-app
Then I went into the folder it created and did:
npx frontity dev
That instantly spins up a site for you to start working with.
To make it feel more real for me, I did went into frontity.settings.js
and changed the source API to point at CSS-Tricks:
{
name: "@frontity/wp-source",
state: {
source: {
api: "https://css-tricks.com/wp-json",
},
},
},
And now look at what I get:
That’s wild. For some projects, that’s straight up ready to deploy.
Check out their intro video which steps through this exact thing
Getting to Work
My first instinct with things like this is to get my hands into the styling right away. The theme that installs by default is the Mars theme and they have a nice guide to help wrap your mind around how it works. The theme uses Emotion for styling, so the components have styles you can mess with right in them. I found the <HeadContainer>
component in index.js
and immediately did the background: red
change!
const HeadContainer = styled.div`
display: flex;
align-items: center;
flex-direction: column;
background-color: red;
`;
It hot-module-reloaded that sucker instantly:
Is this one of those client-side only technologies?
That’s what I thought to myself. I mean, one of the advantages of using WordPress as-is is that you get the server rendering for free. That means no SEO worries (we know client-side rendered sites can take a week or more to be crawled for every change). That means resiliency and speed.
Frontity does do server side rendering! It uses Isomorphic rendering, meaning you need a Node server to render the pages, but that means the browser will get fully formed HTML for pages!
It’s a perfect match for Vercel, basically.
Similarly to how easy a new site is to scaffold and run in development, all you have to do to prep it for production is:
npx frontity build
Then run the Node server:
npx frontity serve
Cool.
I also really like that there is community around all this. If you need help, you’ll get it.
This is a best-of-all-worlds scenario.
I’m always very happy building sites with WordPress, and doubly so now that we have the block editor to use. I really like having an editor experience that helps me write and craft the kind of pages I want to create.
But I also like working with component-based architectures that have fast, easy-to-use, hot refreshing local development environments. Once you work in this kind of dev environment, it’s hard to use anything else! Beautiful DX.
And I also also want to make damn sure the sites I deploy to production are fast, robust, resilient, accessible, and SEO friendly.
I’d get all that with a Frontity site.
Another thing I like here is that Automattic themselves is on board with all this. Not just in spirit, but they are literally big investors. I think they are very smart to see this as an important part of the WordPress ecosystem. Building with WordPress doesn’t mean not building with React, especially with Frontity doing so much of the heavy lifting.
The post Frontity is React for WordPress appeared first on CSS-Tricks.
source https://css-tricks.com/frontity-is-react-for-wordpress/
No comments:
Post a Comment