作者:fover黄瓜小妞1 | 来源:互联网 | 2023-06-29 09:30
IhavebeenworkingonaReactJSprojectwhereIcreatemostofthecomponentsusingflexboxlayout.
I have been working on a ReactJS project where I create most of the components using flexbox layout. Since with react, we can have deeply nested components, so my layout is having nested flexbox layout.
我一直在研究ReactJS项目,我使用flexbox布局创建了大部分组件。由于使用react,我们可以拥有深层嵌套的组件,因此我的布局是嵌套的flexbox布局。
Now my question is, does this have any issue with performance? On a single page, there are many components and each component have 3 to 4 level nested flexbox layout. Will that cause a performance issue?
现在我的问题是,这是否与性能有任何问题?在单个页面上,有许多组件,每个组件都有3到4级嵌套的flexbox布局。这会导致性能问题吗?
2 个解决方案
11
Have done a little test. Rendered 100 components, each with 10 nested layout. With and without flexbox. Here are the code snippets:
做了一点测试。渲染了100个组件,每个组件具有10个嵌套布局。有和没有flexbox。以下是代码段:
Component/index.js
@CSSModules(styles, { allowMultiple: true })
export default class TheComponent extends Component {
render() {
const { deepNest, flex } = this.props
return (
{ this.renderComp(deepNest, flex) }
)
}
renderComp(deepNest, flex) {
const flexProperties = [
{ justifyContent: "center", alignItems: "center" },
{ justifyContent: "flex-start", alignItems: "flex-end" },
{ flexDirection: "row" }
]
const cOntent= [
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus interdum quis ligula vel elementum. Integer non rhoncus purus, eget dignissim ante.",
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus interdum quis ligula vel elementum. Integer non rhoncus purus, eget dignissim ante. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus interdum quis ligula vel elementum. Integer non rhoncus purus, eget dignissim ante."
]
if (deepNest > 0 && flex) {
return (
{ content[deepNest % 3] }
{ this.renderComp(deepNest - 1, flex) }
)
}
if (deepNest > 0 && !flex) {
return (
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus interdum quis ligula vel elementum. Integer non rhoncus purus, eget dignissim ante.
{ this.renderComp(deepNest - 1, flex) }
)
}
}
}
WithFlex/index.js
import TheComponent from "../Component"
@CSSModules(styles, { allowMultiple: true })
export default class WithFlex extends Component {
constructor(props) {
super(props)
this.state = { render: false }
}
render() {
const {render} = this.state
// number of components to render
const arr = _.range(100)
return (
this.setState({render: !render})}>
Start Render
{ render && arr.map((i) =>
) }
)
}
}
WithoutFlex/index.js
import TheComponent from "../Component"
@CSSModules(styles, { allowMultiple: true })
export default class WithoutFlex extends Component {
constructor(props) {
super(props)
this.state = { render: false }
}
render() {
const {render} = this.state
// number of components to renders
const arr = _.range(100)
return (
this.setState({render: !render})}>
Start Render
{ render && arr.map((i) =>
) }
)
}
}
Results from Chrome dev-tool timeline.
WithFlex
WithoutFlex
Summary
The difference is not that much. Also in flexbox, I put random properties to choose from. So I think it's alright with the performance. Hope it will help other devs.
差别不是那么大。同样在flexbox中,我将随机属性选择。所以我认为这对表现来说没问题。希望它能帮助其他开发者。