简体   繁体   English

在React组件内循环

[英]Loop inside a react component

I have the following react component 我有以下反应成分

const Layout = () => {
return (
    <ThemeProvider>
        <Flex wrap>
            <Box width={[1, 1 / 3]}>
                <Text bold>Targeting</Text>

                <Iconlist text="Something"/>

            </Box>
            <Box width={[1, 1 / 3]}>
                <Text bold>Tactics</Text>
            </Box>
            <Box width={[1, 1 / 3]}>
                <Text bold>Results</Text>
            </Box>

        </Flex>
    </ThemeProvider>

)
};
export default Layout;

I want to implement something like this: 我想实现这样的事情:

...
<Iconlist text="Something"/>
<Iconlist text="Something else"/>
<Iconlist text="Something else else"/>
<Iconlist text="Something ...."/>
...

How can I write a loop that can do the above ie display multiple Iconlist component. 我如何编写一个可以完成上述操作的循环,即显示多个Iconlist组件。 I know how to use props to change the "something" value but I am unable to run a loop. 我知道如何使用道具来更改“东西”值,但是我无法运行循环。

I tried this: 我尝试了这个:

<Box width={[1, 1 / 3]}>
                <Text bold>Targeting</Text>

                {
                    for(var i=0;i<=5;i++){
                    <Iconlist text="Something"/>
                }
                }

            </Box>

But I am guessing that's not the correct way to inject javascript in between. 但是我猜这不是在两者之间注入javascript的正确方法。 I am a beginner to React and trying to learn how to do this. 我是React的初学者,试图学习如何做到这一点。 What's the best practice? 最佳做法是什么?

You can use Array.prototype.map() 您可以使用Array.prototype.map()

The map() method creates a new array with the results of calling a provided function on every element in the calling array. map()方法创建一个新数组,并在调用数组中的每个元素上调用提供的函数。

Example

<Box width={[1, 1 / 3]}>
  <Text bold>Targeting</Text>
  { Array(5).map((i) => (<Iconlist key={i} text={`Something ${i}`} />)) } 
</Box>
<Box width={[1, 1 / 3]}>
  <Text bold>Targeting</Text>
    {
      ['something',
       'something else',
       'something else else',
       'something ...',
      ].map((text, index) => <Iconlist key={index} text={text}/>)
    }
</Box>

You need first to declare the text values in array like this 您首先需要这样声明数组中的文本值

const texts = ['something', 'something else' ..]

And in the render method add the following 并在render方法中添加以下内容

{ texts.map((text) => <Iconlist text={text}/>) }

You can store your items in an array in the state : 你可以存储在状态数组的项目:

...

constructor(props) {
  super(props)
  this.state = {
    items: [
      'something',
      'something else',
      'something else else',
      'something ...'
    ]
  }
}

...

...and then map over the array in your render function: ...然后在渲染函数中映射数组:

...

{
  this.state.items.map(item => <Iconlist text={ item } />)
}

...

This will then re-render when you update the state. 然后,当您更新状态时,它将重新呈现。

You could also pass in the array as a prop and then render like this: 您还可以将数组作为prop传入,然后像这样进行渲染:

...

{
  this.props.items.map(item => <Iconlist text={ item } />)
}

...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM