JSX元素的数组未在第二个循环中呈现

发布于 2025-02-02 16:52:04 字数 1120 浏览 3 评论 0 原文

我有一个固体。JS代码,看起来像这样:

import { render, For } from "solid-js/web";

const Text = () => <div style="color: red">Example</div>;

const App = () => {
  const elements = [<Text/>, <Text/>, <Text/>];

  return (
    <div>
      <div>First For Each</div>
      <For each={elements}>{(E) => E}</For>

      <div>Second For Each</div>
      <For each={elements}>{(E) => E}</For>
    </div>
  );
}

render(() => <App />, document.getElementById("app")!);

但是出于某种原因,solid.js仅呈现第二个&lt; for&gt;

”

,当我更改 elements to:

const elements = [() => <Text/>, () => <Text/>, () => <Text/>];

它渲染两次(如果我将 elements 值更改为诸如int或string之类的原始值,也可以正常工作。有人可以向我解释为什么solid.js以这种方式行为?

I have a Solid.js code that looks like this:

import { render, For } from "solid-js/web";

const Text = () => <div style="color: red">Example</div>;

const App = () => {
  const elements = [<Text/>, <Text/>, <Text/>];

  return (
    <div>
      <div>First For Each</div>
      <For each={elements}>{(E) => E}</For>

      <div>Second For Each</div>
      <For each={elements}>{(E) => E}</For>
    </div>
  );
}

render(() => <App />, document.getElementById("app")!);

But for some reason Solid.js only renders the second <For>:

first

And when I change the elements to:

const elements = [() => <Text/>, () => <Text/>, () => <Text/>];

it renders twice (also works fine if I change the elements value to primitive value like int or string. Can someone explain to me why Solid.js behaves this way?

Playground Example

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深缘浅 2025-02-09 16:52:04

编写&lt; text/&gt; 执行返回实际DOM节点的文本组件。 DOM节点只能插入DOM中的一个位置。

将组件执行与函数包裹执行的尝试有效,因为每次执行该函数时,您都会获得不同的元素实例。您基本上是创建组件和HTML元素数组的数组。

这是一个类似的GitHub问题: https://github.com/solidjs/solidjs/solidjs/solid/solid/solid/solid/899

Writing <Text/> executes the Text component which returns an actual DOM node. And dom nodes can be inserted only in one place in the DOM.

The attempt with wrapping the component execution with functions works because you'll get a different element instance every time you execute that function. You're basically creating an array of components vs an array of HTML elements.

Here is a similar Github issue: https://github.com/solidjs/solid/issues/899

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文