javascript - 在条件 for 循环中渲染 React

标签 javascript reactjs

我的网页中有静态信息。

class MyStaticWebPage extends React.Component {
  render() {
    return (
      <TopContainer>
        <IconListContainer>
          <LeftButton
            Icon={MyIcon1}
            color="#ffffff"
            text="text1"
          />
          <CenterButton
            Icon={MyIcon2}
            color="#eeeeee"
            text="text2"
          />
          <RightButton
            Icon={MyIcon3}
            color="#dddddd"
            text="text3"
          />
        </IconListContainer>
        <IconListContainer>
          <LeftButton
            Icon={MyIcon4}
            color="#cccccc"
            text="text4"
          />
        </IconListContainer>
      </TopContainer>
    );
  }
}

此页面静态显示在行列表中,每行最多三个图标,现在我想动态转动它们,假设我将图标 Prop 存储在 Prop 数组中。

[
  {
    icon: 'MyIcon1',
    color: '#ffffff',
    text: 'text1'
  },
  {
    icon: 'MyIcon2',
    color: '#eeeeee',
    text: 'text2'
  },
  {
    icon: 'MyIcon3',
    color: '#dddddd',
    text: 'text3'
  },
  {
    icon: 'MyIcon4',
    color: '#cccccc',
    text: 'text4'
  }
]

最后使用这个 props 数组使页面自动渲染。

class MyStaticWebPage extends React.Component {
  render() {
    var rows = []
    for (var i = 0; i <= parseInt(iconNum / 3); i++) {
      // row level for loop
      // rows.push(row)
      for (var j = iconNum; j % 3 !== 0; j--) {
        // icon level for loop
        // rows.push(icon)
      }
    }
    return (
      <TopContainer>
        {rows}
      </TopContainer>
    );
  }
}

如何通过实际的 react 代码来做到这一点?

最佳答案

假设您有一个平面数组,但想要将其渲染为三行,您应该做的第一件事就是对数组进行分块。 Lodash 有一个方法可以实现这一点,或者您可以对数组进行足够简单的归约。

const chunkedArray = icons.reduce((reduction, icon, index) => {
  index % 3 ? reduction[reduction.length - 1].push(icon) : reduction.push([icon])
  return reduction
}, [])

现在您的数据形状正确,我们可以轻松地将其映射到输出 jsx。

class IconListWrapper extends React.Component {
  render() {
    const { subList } = this.props
    const buttonTypes = ['LeftButton', 'CenterButton', 'RightButton']
    const Element = buttonTypes[index]
    return (
      <IconListContainer>
        {subList.map((icon, index) => <Element
            Icon={MyIcon1}
            color="#ffffff"
            text="text1"
          />)}
      </IconListContainer>
    );
  }
}

class MyStaticWebPage extends React.Component {
  render() {
    return (
      <TopContainer>
        {chunkedArray.map((subList) => <IconListWrapper subList={subList} />)}
      </TopContainer>
    );
  }
}

关于javascript - 在条件 for 循环中渲染 React,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52362936/

相关文章:

javascript - 将新变量添加到 Prestashop Cart 对象

javascript - 在 Rails 4 应用程序中处理 JS 插件

javascript - Google 是否索引使用 javascript 生成的内容?

reactjs - 高阶组件和 Material-UI makeStyles 的样式问题

javascript - 是否可以创建一个 ES6 Javascript 表达式,其值是具有动态属性名称的对象?

javascript - Firefox 崩溃与 Angular (垃圾收集器巨魔?)

javascript - 错误 : Can't resolve 'path' in 'C:\work\demo\node_modules\mime-types'

ReactJS useState 钩子(Hook) : Can I update array of objects using the state variable itself?

reactjs - 如何使用 React + Next.js 在 Material UI 中使用 Theme?

css - sass 与创建 react 应用程序