praj
praj

Reputation: 859

How to update single value inside specific json array item using redux (Flatlist item click)?

I have a Flatlist with displaying name and status, I want to toggle status of the user by clicking the flatlist item click. I have created the redux action and redux reducer for this purpose .But unable to change the curresponding status.

itemClick (item,index) {
  const value=false;
  this.props.listItemClick({props:'status',value});
  }


 export default (state = INITIAL_STATE, action) => {
     switch (action.type) {
        case LIST_CLICK:
        console.log(action.payload.props);
            return {...state,[action.payload.props]: action.payload.value};
        default:
            return state;

    }
};

but not able change status corresponding to specified user. for example Raj status to false

{
"response": [{
  "name": "RAJ",
   "status": true

 }, {
 "name": "RAM",
  "status": true,

}]
}


  <FlatList
     showsVerticalScrollIndicator={false}
     removeClippedSubviews={false}
     data={this.props.response}
     ItemSeparatorComponent = {this.flatListItemSeparator}
     ListFooterComponent={this.flatListItemSeparator}
     keyExtractor={(item, index) => index}
     renderItem={({item,index}) => this.renderFlatListItem(item,index)}/>

Upvotes: 0

Views: 1220

Answers (1)

Tim
Tim

Reputation: 10719

I don't know if this is exactly what you are trying to achieve, but i'll give it a try.

//call Action 
this.props.listItemClick('RAJ',false});

//Action
export const listItemClick = (user,status) => {
    return {
        type: LIST_CLICK,
        payload: { user, status }
    };
}; 

 export default (state = INITIAL_STATE, action) => {
     switch (action.type) {
        case LIST_CLICK:
            //replace YOUR_DATA with your state var 
            return {...state, YOUR_DATA: [state.YOUR_DATA.map((val,index) => {
                   if (val.name === action.payload.user) {
                      return { ...val, status: action.payload.status };
                   }
                   return { ...val };
            })]};
        default:
            return state;

    }
};

Upvotes: 1

Related Questions