- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我当前将一个组件数组渲染为另一个组件的子组件,如下所示:
const myComponents = [
<div key='1'>Component 1</div>,
<div key='2'>Component 2</div>,
<div key='3'>Component 3</div>,
];
render() {
return (
<AnotherComponent>
{myComponents}
<div>Another element</div>
</ AnotherComponent>
)
}
我的问题是,在 AnotherComponent
中,我的组件数组呈现为数组(这是有道理的):
// Inside AnotherComponent
console.log(this.props.children) // [[ {}, {}, {} ], {} ]
我的预期输出是组件应该“传播”,如下所示:
// Inside AnotherComponent
console.log(this.props.children) // [ {}, {}, {}, {} ]
我尝试在渲染中映射
组件,但输出是相同的:
const myComponents = [
...
];
render() {
return (
<AnotherComponent>
{myComponents.map(component => component)}
<div>Another element</div>
</ AnotherComponent>
)
}
我可以通过展平 AnotherComponent
中的子数组来实现这一点。但我想知道是否有一种语法可以让我之前将其展平,以便 AnotherComponent
更干净。
我想实现这一点的原因是因为我在 AnotherComponent
内进行了一些复杂的布局渲染。这要求子级不能是数组,除非我明确想要它,但这里不是这种情况。
编辑:这是一个包含我当前输出和预期输出的演示:
const components = [
<div>Component 1</div>,
<div>Component 2</div>,
<div>Component 3</div>,
];
const MyLayoutComponent = ({ children }) => {
console.log('❌ Current output', children);
console.log('✅ Expected output', children.flat());
return <div>{children}</div>
}
const App = () => (
<MyLayoutComponent>
{components}
<div>Some other component</div>
</MyLayoutComponent>
);
ReactDOM.render(<App />, document.body)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.0.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.0.0/umd/react-dom.production.min.js"></script>
最佳答案
事实上,它们以数组的形式出现是合适的,而且很有用,因为如果列表的顺序根据 key
属性发生变化,React 可以优化重新渲染。事实上它们位于一个数组中,这使得 React 寻找一个键。
如果您不希望它们本身成为列表,您可以使用片段代替:
const myComponents = <>
<div>Component 1</div>
<div>Component 2</div>
<div>Component 3</div>
</>;
这仍然会作为 props.children
中的单个条目出现,但该条目将是一个片段,而不是一个数组,并且 React 不需要其上的键等。
我能想到的唯一其他答案是将其他组件也放入一个数组中(但这需要它们有一个key
):
const App = () => (
<MyLayoutComponent>
{[...components, <div key="other">Some other component</div>]}
</MyLayoutComponent>
);
const components = [
<div>Component 1</div>,
<div>Component 2</div>,
<div>Component 3</div>,
];
const MyLayoutComponent = ({ children }) => {
console.log('✅ Current child count', children.length);
console.log('✅ Expected child count', children.flat().length);
return <div>{children}</div>
}
const App = () => (
<MyLayoutComponent>
{[...components, <div key="other">Some other component</div>]}
</MyLayoutComponent>
);
ReactDOM.render(<App />, document.getElementById("root"));
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.0.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.0.0/umd/react-dom.production.min.js"></script>
或者在这种情况下放弃 JSX 并直接编写 createElement
调用:
const App = () => React.createElement(
MyLayoutComponent,
{
children: [
...components,
<div>Some other component</div>
]
}
);
const components = [
<div>Component 1</div>,
<div>Component 2</div>,
<div>Component 3</div>,
];
const MyLayoutComponent = ({ children }) => {
console.log('✅ Current child count', children.length);
console.log('✅ Expected child count', children.flat().length);
return <div>{children}</div>
}
const App = () => React.createElement(
MyLayoutComponent,
{
children: [
...components,
<div>Some other component</div>
]
}
);
ReactDOM.render(<App />, document.getElementById("root"));
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.0.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.0.0/umd/react-dom.production.min.js"></script>
我想不出 JSX 方法可以做到这一点。
关于javascript - 在 React 中的渲染中传播一组组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61474340/
如果有人能解释这个注释的作用以及我们何时使用它: @Transactional(propagation=Propagation.REQUIRED) 谢谢 最佳答案 如果您需要在 Spring Docs
我有一个页面,它有一个 keydown 事件监听器,用于监听 Escape 键,以便返回。我还有一个简单的模态类,它也监听 Escape 键以关闭它。主页监听器检查模式是否打开,如果打开,则不执行任何
我想在模型中设置默认变量名称 T (=xx) - 将该模型拖到新模型中并在其中定义变量 xx。我收到错误消息:使用未声明的变量 xx。 这是子模型 model test parameter Rea
在 android 2.x 浏览器中查看此示例..它是在我的应用程序中复制场景的示例.. http://johnchacko.net/samples/tap.html 它是关于监听“tap”并从监听器
如您所见,我正在尝试将 GatewayConnectionFailedException 传播到我的 UI。我希望此代码捕获除异常之外的所有内容,我希望表示层捕获该异常以通知用户数据库是问题所在,以便
我目前正在尝试让可执行文件与它需要的所有依赖项正确链接。 这是依赖项的示例结构: exe -> libA -> libB exe和 libA有自己的存储库。 exe拉入libA像这样的东西: add_
有什么方法可以调用带有单个参数的 Scala 函数,给定一个数组 (类似于 JavaScript Spreads在 ECMAScript 6) 中? ys = [10.0, 2.72, -3.14]
我有一个小型静态库,它需要 boost 头文件,并且需要包含目录中的“include”目录。 ... add_library(alib STATIC ...) target_include_direc
我有一些 promise 可以返回对象。 现在我想将它们合并/扩展为一个新对象,因此我使用 Lodash's extend . var whenEverythingIsDone = Promise.a
这是我认为人们通常希望在 Scala 中做的事情,但如果我能在任何地方找到一个例子,我就该死了。 这段代码由于类型删除而无法编译,但它演示了我正在努力完成的事情: def parse[T](json:
这是我认为人们通常希望在 Scala 中做的事情,但如果我能在任何地方找到一个例子,我就该死了。 这段代码由于类型删除而无法编译,但它演示了我正在努力完成的事情: def parse[T](json:
我们有大量 MOSS 2007 站点需要添加大量的 javascript。我编辑、 checkin 、发布并批准了对 default.master 的更改,更改反射(reflect)在根网站上,但没有
请看一下下面的 fiddle :http://jsfiddle.net/K9NjY/ 我在这段代码上花了 3-4 个小时,并将其缩小到最短的版本,但现在我陷入了困境。 问题:1. 点击“divOne”
我读到如果在流程中抛出异常,框架要做的第一件事就是检查消息头中的错误 channel 属性。总是这样吗? 在我的特殊情况下,我将自定义错误 channel 分配给消息 header ,但该消息似乎已向
创建一个小的 C++ 大型精度类,一切似乎都运行良好,但是添加,如果我将 0xffffffff 和 0x04 加在一起,我会得到 0xffff0003,而我应该得到 0x0100000003。这是有问
我正在尝试重新创建 Dan Abramov 类(class)中的 Redux 示例。传播{...store.getState()}在应用程序级别不起作用,Redux 正在更改状态并且 React 不会
考虑一个需要很长时间的事务。在此期间,我想对 TableSmall 执行一些小更新。 ,它应该立即执行,并且主事务的回滚不应该回滚那些小的更新。 我当前的问题是这些小更新将锁定 TableSmall\
我需要对现有函数进行修改,具有一些 const 输入参数: int f(const owntype *r1, const owntype *r2) 为了做到这一点,我想调用一个使用相同类型但没有 co
我有一个带有 ViewModel 的 WPF UserControl: 这个 UserControl 有一个 De
我试图在收到这样的短信时不传播 public class SMSReceiver extends BroadcastReceiver { @Override public void onRec
我是一名优秀的程序员,十分优秀!