I am working on a react app where I have implemented a onClick button but that onClick button is deleting all the elements in the redux store array even when I am trying to filter and delete.Here are my codes:
reducerFunction
case Actions.DELETE_API_GRANT:{
return{
...state,clientAccessGrants:[state.clientAccessGrants.filter(item=>action.grant.api!==item.api)]
}
}
My UI Component:
deleteApiGrants(grant)
{
this.props.deleteApiGrants(grant)
}
{this.props.clientAccessGrants.map((grant, index) => (
console.log(grant.api),
<tr key={grant.api+grant.group}>
<td>{grant.api}</td>
<td>{grant.group}</td>
<td>
<div>
<input type='button' value="delete" className='btn' onClick={()=>this.deleteApiGrants({api:grant.api,group:grant.group})}></input>
</div>
</td>
</tr>
My array object structure is:
{
api:"something",
group:"something"
}
My map and dispatch function:
const mapDispatchToProps = (dispatch) => ({
deleteApiGrants:(grant)=>dispatch(onDeleteApiGrant(grant))
});
const mapStateToProps = (state) => ({
clientAccessGrants:state.client.clientAccessGrants
});
My ActionCreator:
export function onDeleteApiGrant(grant)
{
console.log(grant)
return {type:Actions.DELETE_API_GRANT,grant}
}
Any help will highly appreciated.
Thanks
*.filter(...)in square brackets. filter returns an array, and you're creating array of array, which results in invalid data.Array.filter(...)will create a new reference already.