Issue #1970Opened April 21, 2019by rodenp33 reactions

Integrating React components

Question

Firstly what a great tool you have here.

I'm sorry if i ask questions that have been asked before. I have tried to read through the documentation and googled posts but am still a bit stuck.

I am like a few people trying to integrate React components into grapesjs.

https://github.com/artf/grapesjs/issues/170 mentions custom render logic. How would i implement custom render logic?

and i found this -> https://www.npmjs.com/package/grapesjs-react which does not contain any actual draggable components that i could see.

Are there any examples that actually have a sample react component that can be dragged onto the canvas?

Can you outline the areas that will need to be addressed when integrating react components.

Thanks again for an awesome project.

Answers (3)

rodenpNovember 11, 201912 reactions

Fantastic work @beepsoft Integrating React properly into the core would be a great step and could make way for the support of other frameworks. This will raise the bar for grapesjs to another level.

artfOctober 19, 201911 reactions

Really cool @beepsoft especially all the explanation of the process

Could you please add a public API to be able to provide a custom HTML parser the same way as it is possible to provide a custom CSS parser?

I'll see if I'll be able to add something quickly without too much refactoring but from what I see your way is quite safe even if you're using not public methods.

Anyway, the project you've made is able to render one only component, the <Timer/>, I hardly believe anyone will ever jump in such a journey just to add one React component. I think that we have to create a kind of generic React component renderer that will be able to display automatically any React component.

An example of what I'd expect as an API

import grapesjs from 'grapesjs';
import ReactRenderer from 'grapesjs-react-renderer';

grapesjs.init({
	...
	plugins: [
		// this could contain the custom HTML parser, code generator, commands, etc.
		ReactRenderer,
	],
});

// ...
// somewhere in the code or another plugin
import ReactComponent from 'some/react/component';
import { addComponent } from 'grapesjs-react-renderer';

// ...
addComponent(editor, ReactComponent, {
	name: 'Component name',
	block: { // eg. add also a block
		label: 'Block name',
		media: '<svg ...',
	},
	traits: [ ... ], // Add custom traits or build them by reading the component propTypes
	// ... other options
});
beepsoftOctober 18, 201910 reactions

Hi,

I created a poof of concept implementation/example of the integration of a React component as a GrapesJS block/component here:

https://github.com/beepsoft/grapesjs-react-component-example

Although "integrating with React" could mean many things here's the use case I tried to solve:

  • Have a GrapesJS block, which when dragged onto the canvas uses a React component to display the content in the canvas.
  • Have the same component generate JSX code of itself in the final template.
  • Have a way to get the GrapesJS generated JSX/CSS as text and make it live again by loading the JSX/CSS text into a React component.

For all this to work I had to implement a custom HTML parser and replace the built-in parser somehow, but the way I did is kind of hackish. @artf Could you please add a public API to be able to provide a custom HTML parser the same way as it is possible to provide a custom CSS parser?

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.