react-refetch icon indicating copy to clipboard operation
react-refetch copied to clipboard

Allow returning a `null` mapping from a lazy fetch?

Open ianstormtaylor opened this issue 7 years ago • 2 comments

Hey, I'm looking at a use case that isn't covered currently. I've got a handful of different lazy fetches on a settings page, and a few of them are "dangerous" such that they require user confirmation. I was thinking it would be really nice to be able to do:

function confirmAndDeleteField(field) {
  const yes = window.confirm(`Are you sure you want to delete the "${field.name}" field?`)
  if (!yes) return null
  return {
    url: `/api/fields/${field.id}`,
    method: `DELETE`,
    then: () => ...
  }
}
...
<button onClick={e => this.props.confirmAndDelete(field)}

But right now "aborting" the lazy fetch before it starts by returning null throws an error.

I realize I could solve this by adding a separate function to the view itself, and handling the confirm there, and then calling into the props. function afterwards. But it feels like added complexity, when return null feels like an elegant solution.

What do you think @ryanbrainard?

ianstormtaylor avatar Jan 25 '17 22:01 ianstormtaylor

While I think it is better probably handle this in the component, I can also see that returning an empty/null/undefined mapping should probably be a no-op instead of an error. Going to think on it a bit, but we could follow the same pattern as then that no-ops on undefined. Would that be an acceptable solution?

ryanbrainard avatar Jan 25 '17 22:01 ryanbrainard

@ryanbrainard yup undefined sounds great to me!

ianstormtaylor avatar Jan 25 '17 23:01 ianstormtaylor