Can a theme be built in React?

Can a theme be built in React?

You can use React in a theme yes, if your theme only has the one template and relies on JS to render the entire frontend and navigate, pull in data, then I don’t see how it’s any different if you use or don’t use React

Would there be any reason why the front end of the site wouldn’t work?

You’d have to account for various URLs and loading data in your react app, but at that point it’s a React problem, not so much a WP problem.

The React app would call the API so I don’t see what the issue would be if I called http://foobar.com/wp-json/wp/v2/posts/

No problem here

The one concern I have is that you’ll need to manage routing yourself as you click around the application, and make sure that when you visit something that isn’t the homepage on the first load, that it loads the correct content. But that’s a React Router problem ( assuming thats the library you use for routing )

WP instance on a sub-domain and a React site on the main-domain.

This sounds like a great way of unnecessarily complicating things. Just have the theme serve the React app. Loading resources from within a theme is the same problem if it’s React or jQuery or backbone, and the browser loads them the same way. Just tell your JS where the theme URL is.

Would there be anything that would cause issues in a React-WordPress theme?

This is an extremely open ended question and would depend on what you’re trying to do.

In all honesty it sounds like you’re looking for reassurance. Remember, React is javascript. You can use javascript in themes, and you can call the REST API from javascript in a theme. It doesn’t matter what UI framework is powering that code, if it’s React, Vue, jQuery, vanilla JS, angular, it’s all JS.

If you want to handle loading posts and rendering them in your javascript then great, you can do that. If you choose React as the means to do that then that’s up to you.

There are a few obvious things you’ll need to do though:

Just keep in mind that you’ll want to call wp_footer and wp_head, so you’ll need a header.php and footer.php, you’ll also want a functions.php so you can enqueue your JS and styles.

None of that is React specific though, every theme should do that. You might also want to enqueue the WP API script so that it passes the REST API url over. Again that’s not specific to a React application.

But most importantly, there is no such thing as a React theme. There are themes that just happen to use React. It’s easy to think of it as something different, but it’s really just JS in a rather empty looking theme at the end of the day

Leave a Comment