craft.js icon indicating copy to clipboard operation
craft.js copied to clipboard

Nested "type" key in Element props leads to serialization error

Open Jank1310 opened this issue 4 years ago • 4 comments

The following props will cause a "Cannot convert undefined or null to object" error.

<SomeElement foo={{type:"bar"}} />

I suppose the following line causes it: https://github.com/prevwong/craft.js/blob/2b09e3824cfb4f3b660a3f3bb14a14aba108ef34/packages/core/src/utils/serializeNode.tsx#L34

It the type key is nested one level deeper everything works as expected:

<SomeElement foo={{bar:{type:"bar"}}} />

Jank1310 avatar Mar 21 '21 15:03 Jank1310

I'm facing this issue too:

Screen Shot 2021-09-30 at 09 27 51

We integrating a rich text editor (tiptap) as a Craf.js block. A Tiptap document has a type attribute in every node, causing this issue to raise after any attempt to change the document.

Another case was storing an image, where we had a type attribute for the mime type. We got it working by renaming it to mime.

Any ideas on how to solve it or updates on this issue?

And of course: Thanks for this awesome library!

eduludi avatar Sep 30 '21 07:09 eduludi

I just faced the same issue as well. My component accepts a prop with this shape:

mediaSource?: { type: 'token'; token: string } | { type: 'library'; uuid: string };

By changing from type to source the issue went away.

achendrick avatar May 19 '22 17:05 achendrick

@eduludi did you end up finding a fix for this? Facing the same issue while using tiptap editor.

iamtekeste avatar Jul 14 '22 00:07 iamtekeste

Why is this function recursively looking for "type" and "children"? I couldn't find a test case that illustrates that point.

Based on my understanding of the data model, it makes no sense to treat these as anything but user data because these represent React node properties when serializing.

ryanackley avatar Nov 27 '22 10:11 ryanackley