【问题标题】:How to handle state on Flat List Items in React-Native如何在 React-Native 中处理平面列表项的状态
【发布时间】:2019-05-31 20:05:48
【问题描述】:

我正在为 React-Native 的 FlatList 组件的项目设置一个计数器。每次用户按“+”或“-”按钮时如何更新列表项?

我目前能够更新状态的值,但是列表不显示新状态。我尝试将 extraData 组件添加到 FlatList,但它似乎并没有更新。

这是数据结构

      data: [
        {
          id: 1,
          name: "Bread",
          price: "400",
          imageS: "../resources/pan-corteza-blanda.jpg",
          quantity: 2
        },
... more data

这是处理增量的函数

  handleIncrement = i => {
    this.setState(state => {
      const formatData = state.data.map((item, j) => {
        console.log("Id", i + " /// " + item.id);
        if (item.id === i) {
          item.quantity = item.quantity + 1;
          return item;
        } else {
          return item;
        }
      });
      console.log("FormatData" + formatData); //Displays the correct  quantity of the item updated

      return {
        formatData
      };
    });
  };

这是列表组件

  <FlatList
          data={this.state.data}
          style={styles.list}
          extraData={this.state.data}
          renderItem={this.renderItem}
        />

我希望每次用户按下“+”或“-”按钮时都使用正确的数量值更新列表项的文本组件。

【问题讨论】:

    标签: reactjs react-native react-native-flatlist react-state-management


    【解决方案1】:

    您需要更新数据状态而不是返回项目。

    handleIncrement = i => {
    
      const item = this.state.data[i];
    
      this.setState({
        data: [
          ...this.state.data.slice(0, i),
          Object.assign({}, this.state.data[i], { quantity: item.quantity + 1 }),
          ...this.state.data.slice(i + 1)
        ]
      });
    };
    

    您可以重构该函数并将其用于-+

    // pass array index and quantity 1 for + and -1 for -
    handleIncrement = (i, qty) => {
      const item = this.state.data[i];
    
      if (item && item.quantity === 0 && qty === -1) {
        return;
      }
    
      this.setState({
        data: [
          ...this.state.data.slice(0, i),
          Object.assign({}, this.state.data[i], { quantity: item.quantity + qty, }),
          ...this.state.data.slice(i + 1),
        ],
      });
    };
    

    下面是使用上述功能的演示,它在reactjs中。该函数也可以在 react native 中工作。

    h1, p {
      font-family: Lato;
    }
    
    .container {
      display: flex;
      flex-direction: row;
      border-bottom-style: solid;
      margin-bottom: 5px;
    }
    
    .image-container {
      flex-grow: 0;
    }
    
    .info-container {
      display: flex;
      margin-left: 10px;
      flex-direction: row;
    }
    
    .title {
      margin-top: 0;
    }
    
    .titleContainer {
      width: 100px;
    }
    
    .cover {
      width: 30px;
    }
    
    .buttons {
      flex-grow: 1;
      display: flex;
      margin-left: 10px;
    }
    
    .incrementButtons {
      width: 20px;
      height: 20px;
      margin-left: 10px;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.0/umd/react.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.0/umd/react-dom.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script>
    <div id="root"></div>
    
    <script type="text/babel">
    
    class Item extends React.Component {
      render() {
        return (
          <div className="container">
            <div className="image-container">
              <img className="cover" src={this.props.image} />
            </div>
            <div className="info-container">
              <div className="titleContainer">
                <p className="title">{this.props.title}</p>
              </div>
              <div className="buttons">
                <p className="title">{this.props.qty}</p>
                <img onClick={() => this.props.increment(this.props.index, -1)} className="incrementButtons" src="https://img.icons8.com/metro/26/000000/minus-math.png" />
                <img onClick={() => this.props.increment(this.props.index, 1)} className="incrementButtons" src="https://img.icons8.com/metro/26/000000/plus-math.png" />
              </div>
            </div>
          </div>
        )
      }
    }
    
    class App extends React.Component {
    
      state = {
        data: [
          {
            id: 1,
            name: 'Avocado',
            price: '400',
            imageS: 'https://img.icons8.com/metro/26/000000/avocado.png',
            quantity: 0,
          },
          {
            id: 6,
            name: 'Bread',
            price: '300',
            imageS: 'https://img.icons8.com/metro/26/000000/bread.png',
            quantity: 0,
          },
          {
            id: 2,
            name: 'Milk',
            price: '300',
            imageS: 'https://img.icons8.com/metro/26/000000/milk-bottle.png',
            quantity: 0,
          },
        ],
      };
    
      handleIncrement = (i, qty) => {
        const item = this.state.data[i];
    
        if (item && item.quantity === 0 && qty === -1) {
          return;
        }
    
        this.setState({
          data: [
            ...this.state.data.slice(0, i),
            Object.assign({}, this.state.data[i], { quantity: item.quantity + qty, }),
            ...this.state.data.slice(i + 1),
          ],
        });
      };
    
      render() {
    
        const items = this.state.data.map((item, index) => (
          <Item qty={item.quantity} index={index} key={index} increment={this.handleIncrement} title={item.name} image={item.imageS} />
        ))
        return (
          <div>
            {items}
          </div>
        );
      }
    }
    
    ReactDOM.render(<App />, document.getElementById('root'));
    </script>

    【讨论】:

    • 如果此解决方案适合您,请点击我的答案旁边的向右箭头接受我的答案。
    • 完美!非常感谢我的朋友。
    • 我遇到过类似的问题,你能检查一下这个 Q,请@JuniusL。 stackoverflow.com/questions/57595796/…
    【解决方案2】:

    this.state.data 永远不会在您的 handleIncrement 上更改,这就是您传递给 FlatList 的内容。这就是FlatList 不更新的原因。

    handleIncrement 运行后,您的状态唯一改变的是:

    {
      formatData: [...stuff here]
    }
    

    也许您想改为传递 this.state.formatData 或将其在 handleIncrement 中重命名为 data

    此外,您的状态结构可能会更好作为地图,其中键是 itemId。这样,您每次想要增加数量时都不需要映射整个列表。

    例如

      {
    
        1: {
          id: 1,
          name: "Bread",
          price: "400",
          imageS: "../resources/pan-corteza-blanda.jpg",
          quantity: 2
        },
        // more data...
      }
    

    现在,您的 handleIncrement 看起来像这样:

    handleIncrement = itemId => this.setState(prevState => ({
      ...prevState,
      [itemId]: ++prevState[itemId].quantity
    }))
    

    【讨论】:

      猜你喜欢
      • 2019-02-10
      • 1970-01-01
      • 2020-05-04
      • 1970-01-01
      • 2019-05-02
      • 1970-01-01
      • 2022-01-22
      • 2022-07-12
      • 2019-03-01
      相关资源
      最近更新 更多