How to loop and render elements in React-native?

后端 未结 4 617
粉色の甜心
粉色の甜心 2020-12-24 04:28

Is it possible to loop an identical component in Render function?

Something like this:

...

onPress = () => {
 ...
};

initialArr = [[\"blue\",\"t         


        
4条回答
  •  死守一世寂寞
    2020-12-24 05:27

    You would usually use map for that kind of thing.

    buttonsListArr = initialArr.map(buttonInfo => (
      
    );
    

    (key is a necessary prop whenever you do mapping in React. The key needs to be a unique identifier for the generated component)

    As a side, I would use an object instead of an array. I find it looks nicer:

    initialArr = [
      {
        id: 1,
        color: "blue",
        text: "text1"
      },
      {
        id: 2,
        color: "red",
        text: "text2"
      },
    ];
    
    buttonsListArr = initialArr.map(buttonInfo => (
      
    );
    

提交回复
热议问题