reactjs - 当项目更改时, react native 部分列表不会重新渲染

标签 reactjs react-native react-native-sectionlist

我的 React Native 项目中有一个sectionList。如果项目发生变化,它不会重新渲染。 我的代码:

测试.js

class Test extends React.Component {
started = false;
causeData=[];
showLess=false;
items = [];

_start = () => {
    const { ws } = this.props;

    this.showLess = false;
    if (ws.causes.length) {
  this.causeData = {
    title: Language.causes,
    key: "cause",
    data: []
  };

  ws.causes.forEach(cause => {
    let causeDetails = {
      key: "cause_" + cause.id,
      name: "",
      value: cause.name,
      sortIndex: cause.sortIndex,
      progress: cause.progress
    };
    this.causeData.data.push(causeDetails);

    if (this.causeData.data.length > 4) {
      this.causeData.data = this.causeData.data.slice(0, 4);
    }
  });
  this.items.push(this.causeData);
  console.log("causeData", this.causeData);
  }  
  }
  }

 _renderItem = ({ item }) => {
     return (
          <View>
          <Text key={item.key} style={styles.text}>{`${item.name}  ${
            item.value
          }`}</Text>
        </View>
  );
 };

_renderSectionHeader = ({ section }) => {
   const { ws } = this.props;
   const showMore = ws.causes.length > 0 && !this.showLess;

  return (
    <View style={styles.sectionHeader}>
      <Text key={section.key} style={styles.header}>
        {section.title}
      </Text>
      {showMore && (
        <Button
          onPress={this._afterCauseAnswered}
          title={Language.showMore}
          data={this.items}
          accessibilityLabel={Language.causeDoneAccessibility}
        />
      )}
      </View>
    );
    };

   _keyExtractor = (item, index) => item.key;

  _afterCauseAnswered = () => {

    const { stepDone, ws } = this.props;
    this.causeData.data = { ...ws.causes };

    stepDone("showMoreAnswered");
    this.showLess = true;
  };

  render = () => {
  if (!this.started) {
  this.started = true;
  this._start();
  }
  return (
  <View style={styles.container}>
    <SectionList
      sections={this.items}
      extraData={this.items}
      renderItem={this._renderItem}
      renderSectionHeader={this._renderSectionHeader}
      keyExtractor={this._keyExtractor}
    />
  </View>
);
};
}

在我的部分列表部分标题中包含一个名为 showMore 的按钮。初始渲染时,它只会显示 5 个项目,而单击 showMore 时,它​​应该显示所有列表。这是我的功能。但是,当单击“showMore”按钮时,它不会显示整个列表,仅显示 5 个项目,这意味着sectionList 不会重新渲染。如何解决这个问题?我是本地 react 新手。知道我错过了什么吗?任何帮助将不胜感激!

最佳答案

itemsshowLess 保持在某种状态,并在按下按钮后使用新值调用 setState。它将重新呈现 SectionList。另外,如果您想通过显示列表显示多个项目,则需要将 showLess 移动到 item 元素,以便每个项目都知道如何显示它。

关于reactjs - 当项目更改时, react native 部分列表不会重新渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55531049/

相关文章:

javascript - 为什么用成功 promise 调用 catch ?

php - 如何简化 php API 中格式化数组列表数据以提供 React Native 部分列表

reactjs - 如何更改 react 中 Prop 的值?

python - Django 休息和 react : lookup_field = 'slug' not returning item from db

javascript - Mobx- react : Inject and Observe together

javascript - 调用SectionList的scrollToLocation方法后,scroll在跳跃

reactjs - 我如何构建这个 React Native 部分列表实现

reactjs - 为什么我无法使用 React 更新 Swiper Slide 中当前的索引状态

javascript - react native : Is there any way to check if the file exists in android_asset folder or not?

react-native - 为什么 React Native < Text > 组件不显示 bool 属性?