react-data-fetching icon indicating copy to clipboard operation
react-data-fetching copied to clipboard

Enable to pass in a side-effect

Open busypeoples opened this issue 6 years ago • 2 comments

Opening an issue for documentation purposes. A PR covering this issue will follow. Sorry @CharlesMangwa for delaying this for some time now. Enable to pass in a callback that react-data-fetch either runs on componentDidMount or via passing down a run function f.e., enabling to define when the fetch is run in user land. This should solve the following issues:

  • Being able to run multiple fetches serially or in parallel
  • Enable to delay or re-run a fetch when needed
  • Best case is implementing this in a fashion where we can run Tasks or Promises, leaving the actual implementation to user land.

The following is not the actual implementation, but an initial idea.

const dataFetch = () => {
     return fetch(SOME_URL_ENDPOINT)
      .then(response => response.json());
}

export default class App extends Component {
  render() {
    return (
      <Fetch
        loader={<Loader/>} // Replace this with your lovely handcrafted loader
        run={dataFetch}
      >
        {({ data, run /* can also be passed down and used inside a class component f.e. */ }) => (
         <div>
          <h1>Username</h1>
          <p>{data.name}</p>
         </div>
        )}
      </Fetch>
    )
  }
}

busypeoples avatar Jun 29 '18 22:06 busypeoples

Hey @busypeoples! Thanks for taking the time to write this down! Since you've already sent your PR, let's continue the discussion there! #22

CharlesMangwa avatar Aug 18 '18 00:08 CharlesMangwa

Sorry, misclicked!

CharlesMangwa avatar Aug 18 '18 00:08 CharlesMangwa