Dispatch this action to issue a DELETE
request to your API.
Endpoint calculation for deleteEntity
is the same as for updateEntity
. It is resolved from type
and id
of the resource object passed to this action creator.
The following resource object will resolve to "/tasks/1":
{
"type": "tasks",
"id": "1",
"attributes": {
"task": "New task name"
}
}
import { connect } from 'react-redux';
import { updateEntity } from 'redux-json-api'
const mapStateToProps = ({
api: { tasks = { data: [] } }
}, { taskId }) => ({
task: tasks.data.find(task.id === taskId)
});
class DeleteTask extends Component {
deleteTask() {
const { dispatch, task } = this.props;
dispatch(deleteEntity(task));
}
render() {
return <button onClick={this.deleteTask.bind(this)} />
}
}
export default connect(mapStateToProps)(DeleteTask);
Immediately upon dispatching deleteEntity
, the API_WILL_DELETE
action will be dispatched. This will increment state.api.isDeleting
and set isInvalidating
to "IS_DELETING" on the resource object passed to the action creator.
When the API responds successfully, API_DELETED
is dispatched to remove the resource object from state. state.api.isDeleting
will be decremented.
We will also resolve the returned Promise when this action is dispatched.
Is the API returns an error during a DELETE request, this action will be dispatched. It will decrement state.api.isDeleting
and throw an error in the returned Promise.