简体   繁体   English

如何在状态/道具更改中的react-native flatList中重新呈现特定行?

[英]How to re-render a particular row in react-native flatList on state/props changes?

I am new to react-native and redux.I have a flatList to show a list details from an api call using redux.Whenever I try to edit/delete a list item,flatList does not re-render upon the changes. 我是react-native和redux的新手。我有一个flatList来显示使用redux的api调用的列表详细信息。每当我尝试编辑/删除列表项时,flatList不会在更改时重新呈现。

I have tried with extraData props also,but nothing worked out.Seriously looking for help!! 我也尝试过extraData道具,但没有成功。很想找到帮助!!

 <FlatList
                data={this.state.vacationsDataSource}
                keyExtractor={(item, index) => index}
                extraData={this.props.Vacations_reducer.vacationsData}
                renderItem={({ item }) => (
                  <View style={styles.mainView_contact}>
                    <View style={{ flexDirection: "row" }}>
                      <View style={styles.Txt_hight}>
                        <Text style={styles.SecName}>Start date </Text>
                      </View>
                      <View>
                        <Text style={styles.name2}>
                          {dateFormat(item.vacationFrom, "mmmm dS, yyyy")}
                        </Text>
                      </View>
                    </View>

Here is my componentWillRecieveProps method, 这是我的componentWillRecieveProps方法,

componentWillReceiveProps(nextProps) {
    if (this.props.Vacations_reducer.vacationsData != nextProps.Vacations_reducer.vacationsData) {
      this.setState({
        vacationsDataSource: nextProps.Vacations_reducer.vacationsData
      });
    }
  }

My componentDidMount, 我的componentDidMount,

componentDidMount() {
    this.props.onRenderFetchVacations().then((res) => {
      this.setState({
        vacationsDataSource: this.props.Vacations_reducer.vacationsData
      });
    });
    Keyboard.addListener("keyboardDidShow", this._keyboardDidShow);
    Keyboard.addListener("keyboardDidHide", this._keyboardDidHide);
    BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
  }

And my reducer looks like this, 我的减速机看起来像这样,

const VacationDetailReducer = (state = DEFAULT_STATE_VAC, action) => {
  switch (action.type) {
    case VAC_FETCH_LOADING:
      return {
        ...state,
        isLoading: true
      };

    case VAC_FETCH_SUCCESS:
      return {
        ...state,
        isLoading: false,
        vacationsData: action.vacationsData
      };

    case VAC_FETCH_FAIL:
      return {
        ...state,
        isLoading: false,
        vacationsData: action.vacationsData
      };
case ADD_VAC_FETCH_SUCCESS:
      return {
        ...state,
        //isLoading: false,
        addVacationData: action.addVacationData
      };

    case ADD_VAC_FETCH_FAIL:
      return {
        ...state,
        //isLoading: false,
        addVacationData: action.addVacationData
      };

    case UPDATE_VAC_FETCH_SUCCESS:
      return {
        ...state,
        //isLoading: false,
        updateVacationData: action.updateVacationData
      };

    case UPDATE_VAC_FETCH_FAIL:
      return {
        ...state,
        //isLoading: false,
        updateVacationData: action.updateVacationData
      };

    case DELETE_VAC_FETCH_SUCCESS:
      return {
        ...state,
        //isLoading: false,
        deleteVacationData: action.deleteVacationData
      };

    case DELETE_VAC_FETCH_FAIL:
      return {
        ...state,
        //isLoading: false,
        deleteVacationData: action.deleteVacationData
      };

    default:
      return state;
  }
};

export default VacationDetailReducer;

I think ComponentWillReceiveProps doesn't get any updated props, so it doesn't call the setState method and thus the FlatList not rendered again. 我认为ComponentWillReceiveProps没有获得任何更新的道具,因此它不会调用setState方法,因此FlatList不会再次渲染。 You need to check if the ComponentWillReceiveProps is again called or not. 您需要检查是否再次调用ComponentWillReceiveProps。 Just add a console or alert after the if condition. 只需在if条件后添加控制台或警报即可。

componentWillReceiveProps(nextProps) {
   if (this.props.Vacations_reducer.vacationsData != nextProps.Vacations_reducer.vacationsData) {
   console.log("Hello")
   this.setState({
     vacationsDataSource: nextProps.Vacations_reducer.vacationsData
   });
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM