Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

DrupalizeMe/react-and-drupal-examples

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
23 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Drupalize.Me React & Drupal Code Examples

This repo contains the example code used in the https://drupalize.me/series/drupal-8-and-reactjs series.

Note: The hello-world branch contains a trimmed down version of the code that reflects the application as it would be after completing this tutorial covering how to Connect React to a Drupal Theme or Module.

Drupal

The /drupal directory contains a Drupal 10 project with basic configuration for JSON:API, and to demonstrate embedding a React application inside a Drupal theme or module.

See the drupal-9 branch for older examples including Drupal 8/9 and React 16.

Install all the Drupal dependencies:

cd drupal
composer install

Then import the database snapshot in drupal/backup.sql.gz.

The default admin account is admin/admin. You can change this with drush upwd admin {NEW_PASSWORD}.

If you're using ddev this contains ddev configuration and can be started with:

cd drupal
# Start ddev.
ddev start
# Install/update composer dependencies.
ddev composer install
# Import the database snapshot.
ddev import-db --src=./backup.sql.gz
# Run any necessary database updates, and re-import config.
ddev exec "drush updb -y && drush cim -y && drush cr -y"

The /drupal/web/themes/react_example_theme/ contains a custom theme with a React application embedded via the theme. This demonstrates using Webpack to bundle and transpile React/JavaScript files.

cd /drupal/web/themes/react_example_theme;
npm install
# Build the production JS files:
npm run build
# Build development JS files
npm run build:dev
# Start webpack in --watch mode while doing development
npm run start
# Start webpack in --watch mode with hot module reloading
# Requires some config to proxy requests to Drupal see .proxyrc
npm run start:hmr

Decoupled React Application

The /react-decoupled-vite directory contains an example decoupled React application built with Vite. It is built to interact with the API provided by Drupal installed in /drupal. This is the recommended approach.

To download dependencies and start the local development server run:

cd react-decoupled-vite
npm install
npm run dev

You might need to update some configuration to make sure it points to your local Drupal installation. See src/utils/oath.js.

For an older create-react-app based example see the /react-decoupled directory which contains an example of a decoupled React application built with create-react-app.

To download dependencies and start the local development server run:

cd react-decoupled
npm install
npm run start

You might need to update some configuration to make sure it points to your local Drupal installation.

About

Example code for React & Drupal series https://drupalize.me/series/drupal-8-and-reactjs

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Morty Proxy This is a proxified and sanitized view of the page, visit original site.