React Native foreach loop

前端 未结 3 1116
慢半拍i
慢半拍i 2021-02-14 19:09

I\'m developing a little app in React Native and I am looking for something like a foreach function. I just can\'t find a foreach loop. Not on StackOverflow and not even in the

相关标签:
3条回答
  • 2021-02-14 19:43

    In react, preferred way is map method of Array. Example using ES6 arrow function:

    render() {    
        return (
            <View>    
               {dataList.map(r => <Button>{r}</Button>)}    
            </View>
        )
    }
    
    0 讨论(0)
  • 2021-02-14 19:44

    You can use map or for-of or any other

    Example:

    // for of
    for (let userObject of this.state.users) {
        console.log(userObject.username);
    }
    // map
    this.state.users.map((userData) => {
        console.log(userData.username);
    });
    

    as per the error you may not have data within users state, so you are getting error. If data is proper then above example will work properly

    0 讨论(0)
  • 2021-02-14 19:51

    You can also use lodash (npm install lodash) and run the following:

    import _ from 'lodash';
    
    ...
    
    _.each(dataList, function(userObject, key) { console.log(userObject, key); });
    
    0 讨论(0)
提交回复
热议问题