Loop in react-native

前端 未结 6 1631
慢半拍i
慢半拍i 2021-02-04 23:57

I want to make a list of fields depending on the number of the player that user has selected. I wanted to make something like this:

generatePaymentField() {
             


        
6条回答
  •  清酒与你
    2021-02-05 00:52

    You can create render the results (payments) and use a fancy way to iterate over items instead of adding a for loop.

    const noGuest = 3;
    
    Array(noGuest).fill(noGuest).map(guest => {
      console.log(guest);
    });

    Example:

    renderPayments(noGuest) {
      return Array(noGuest).fill(noGuest).map((guess, index) => {
        return(
          
            
            
            
          
        );
      }
    }
    

    Then use it where you want it

    render() {
      return(
         const { guest } = this.state;
         ...
         {this.renderPayments(guest)}
      );
    }
    

    Hope you got the idea.

    If you want to understand this in simple Javascript check Array.prototype.fill()

提交回复
热议问题