Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Can anybody help me set up React in my Grav site?

Solved by Josh View solution

Started by Josh 2 years ago · 6 replies · 510 views
2 years ago

I've tried using Vite to install React in a directory in my Grav theme, but it's not recognized or compiled by the built in Grav (PHP) web server. How do I add React to my Grav site so that my twig template files will recognize and run React and .jsx? Any help is appreciated.

2 years ago

@skipper, The question is quite broad and the provided information is a bit minimal.

It would be handy for us to understand what research you've done, what you've tried, what you expect would happen and where you got stuck... Knowing what skills level you have in building a React frontend in combination with a PHP server in general might also help.

Please have a look at /forum/general/the-art-of-asking-great-questions-t8554. There are a couple of topics that might help improving your question.

2 years ago

Hi. I'm attempting to follow the tutorial on the React docs here:

https://react.dev/learn/add-react-to-an-existing-project

I've tried both suggestions, setting up a modular javascript environment and also via Vite. I need my project (or more specifically my theme) to pull in React and be able to execute javascript (.jsx) files. I can get React in my theme (node_modules in the theme folder via npm) but I get errors when I try to run the following code:

import { createRoot } from 'react-dom/client';
etc. from index.js in the docs

Uncaught TypeError: The specifier “react-dom/client” was a bare specifier, but was not remapped to anything. Relative module specifiers must start with “./”, “../” or “/”.

2 years ago

@skipper, Again, you are not providing much info... Did you read the document I referred to?

In general, apart from loading some js files, generated by the npm build process, into the generated page using the Asset Manager in Twig or PHP, there is not much more for Grav to do.

It seems to be a React and js knowledge issue, for which plenty tutorials and help forums are available on the web.

👍 1
last edited 04/30/24 by pamtbaau
1 year ago Solution

I can answer my own question. I was able to get a React app working inside Grav. It really had very little to do with Grav. The basic steps for me were:

  1. Use webpack and babel to build my React app (.jsx) to native javascript in a bundle.js file.
  2. Import the bundle.js file using the Grav asset manager.

That's sufficient to get a basic React app working. I did a couple other things:

I used the example from example with json files to convert Grav data (a list of child posts) to json, and then brought in the json with fetch(). Then you can do whatever with the data. In my case, I replaced the Tagcloud, SimpleSearch, and Pagination plugins with my own versions in React.

1 year ago

Very interesting. I am curious, if your React app works by itself, what is the purpose of using also Grav?

3 months ago

I used React to consolidate and replace a few Grav plugins, not replace the whole site.

👍 1

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 3 days ago
2 70 5 hours ago
General · by pamtbaau, 10 hours ago
1 45 9 hours ago
General · by Andy Miller, 22 hours ago
0 43 22 hours ago
General · by Marcel, 12 months ago
6 341 4 days ago
General · by Duc , 5 days ago
3 35 5 days ago