surplus icon indicating copy to clipboard operation
surplus copied to clipboard

Fragments?

Open xluk9 opened this issue 6 years ago • 7 comments

Hello, really nice library. I was just playing around and realized that fragments are not supported. Is there any way to implement views without a parent node?

xluk9 avatar Sep 19 '18 16:09 xluk9

Unlike React, Surplus supports returning arrays just fine. So instead of:

    return <>
        <div/>
        <div/>
        <div>
    </>;

... you can do ...

    return [
        <div/>,
        <div/>,
        <div>
    ];

I've got on my list to support the fragment syntax as syntactic sugar for the array version but haven't hit that yet. Hope that helps!

adamhaile avatar Sep 19 '18 16:09 adamhaile

Thank you, that was fast! I was trying with arrays, but I can't make it work. I keep getting content must be Node, stringable, or array of same. I was also looking at surplus-realworld repo to take inspiration. Anyway, this is my index.html file

<html>
    <head>
    </head>
    <body>
    <div id="app"></div>
    <script src="main.js"></script>
    </body>
</html>

and this is main.tsx

import S from 's-js';
import * as Surplus from 'surplus';
import data from 'surplus-mixin-data';

var Name = (name) => [<div>{name}</div>, <div>{name}</div>]
var Page = ( { children }) => <div>{children}</div>

document.getElementById("app").appendChild( Page( { children : Name("Jelly") } ) )

xluk9 avatar Sep 21 '18 10:09 xluk9

Strange, that code looks fine. I tried throwing it into a CodePen, and it appears to run there: https://codepen.io/adamhaile/pen/RYdZXO?editors=1000 .

Maybe something with your build environment? Is it possible to zip up your project (maybe minus node_modules) and attach?

adamhaile avatar Sep 21 '18 12:09 adamhaile

Don't mean to pile on, but I added that code sample to an open project (webpack, typescript) and it worked for me, too. Outputs a div with two inner divs that have the text "Jelly".

Might be helpful to paste the content of main.js along with the project.

derekhawker avatar Sep 21 '18 20:09 derekhawker

It works, strange... I had to remove the compiled file main.js and recompile it.

xluk9 avatar Sep 22 '18 07:09 xluk9

By the way, if you want an actual element instead of using arrays, you can simply make a stub element:

const Fragment = ({children}) => children;

const MyComponent = () => (
    <Fragment>
        <div>Hello</div>
        <div>World</div>
    </Fragment>
);

Qix- avatar May 09 '20 15:05 Qix-

Deleted my comment about this being closeable - Surplus should definitely support the <>...</> syntax :)

I might take a whack at this.

Qix- avatar May 10 '21 01:05 Qix-