React 让我在使用 flexbox 时遇到了麻烦,因为组件渲染函数中的 div 包装脱离了我的 flex-directions。
根据我对 React 的理解,所有组件的 jsx 元素都应该包含在一个 div 中。例如这有效:
return (
<div className="com-container">
<div className="other-stuff">
</div>
</div>
)
而这不会:
return (
<div className="com-container">
</div>
<div className="other-stuff">
</div>
)
然而,这让 flexbox 的行和列变得困难。例如说我们有这个 flex 关系。
<Parent Column Component /> //vertical flex
<ChildRowComponent1 /> //horizontal flex
<ChildRowComponent2 /> //horizontal flex
<ChildRowComponent3 /> //horizontal flex
但是ChildRowComponent2
&3
可以动态换出 - 这导致我通过将ChildRowComponent2
&3
放入它们自己的组件中进行重构,但是由于所有渲染必须包含在单个 div 中,我得到了这个额外的 div,它会混淆 flex 方向。
<Parent Column Component /> //vertical flex
<ChildRowComponent1 /> //horizontal flex
<div>
<ChildRowComponent2 /> //horizontal flex now broken
<ChildRowComponent3 /> //horizontal flex now broken
</div>
问题:
是否有 flexbox 技巧来处理这些情况,或者我只是在构建组件的方式上做错了什么?