Fresh JSX bug? Component children not passed to parent as array

Open
#3,888 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
52/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
typescript
Domain
frontend

Research direction

Start by reproducing the issue with the TestParent component shown in the report, in both an Island and server generated context. Trace how JSX children reach the component and compare them with the workaround that passes an array through the children prop. Done means JSX children arrive as an array that can be handled independently in both contexts.

Written by the indexing model from the issue text.

Description

In standard Preact/Preact, children that are passed to a parent component in jsx style...

<TestParent>
  <h1>Child 0</h1>
  <p>Child 1</p>
</TestParent>

are identified by the parent as an array (of ComponentChild, I suppose).

In Fresh this expected behaviour breaks. Instead, the children are somehow baked into an single object.
This makes it impossible to handle the child items independently - for example mapping over them to wrap each of them in yet another component, or whatever.

It doesn't seem to matter if used in an Island context or server generated.

I guess this must be concidered a bug.

Here below is a test parent component that I've been using to identify the problem:

export function TestParent(
  { children }: { children?: preact.ComponentChildren },
) {
  console.log("Children is array:", Array.isArray(children));
  return (
    <div>
      {children}
    </div>
  );
}

A workaround is to prepare the children as an array, and pass that as children property value...

export default define.page(function Home(ctx) {
  // preparing children as an array
  const children = [
    <h1>Child 0</h1>,
    <p>Child 1</p>,
  ];

  return <TestParent children={children} />;
});

...but that's not always possible.

Dominant language
TypeScript
Stars
13.8k
Forks
757
Avg merge
3m
Merged PRs (30d)
2

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from freshframework/fresh

All issues in freshframework/fresh

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.