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)
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.
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
});
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.
Issue #2570
[QUESTION] 3rd party RTE integration
Hi there! Great work at grapesjs! Is there any list of external RTE's to integrate in grapesjs? I've spent a few days trying to integrate a...
Issue #358
Calling updateScript from the script section of a pluging
Hi guys, I've been stuck for a few hours trying to figure out how to call updateScript from the script section of a custom plugin. So far I...
Issue #429
custom component save
hi @artf sorry to be a pain i really didn't want to open a issue ticket but i have been stuck for days trying to achieve my end goal i have...
Issue #769
Need further info to create backend for Asset Manager
I'm trying to figure out how to write a backend (in ASP.NET Core) for the Asset Manager. I've looked at a few of the Wiki pages, but still...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.