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

Load from URL easily

Open RoyalIcing opened this issue 8 years ago • 3 comments
trafficstars

export const load = '/api/photos'

RoyalIcing avatar Jun 16 '17 09:06 RoyalIcing

  • Fix typo above
  • How to make use of props? Smartly handle prop changes?
  • How to set state’s key?
  • Integrate with GraphQL?
export const load = {
  items: '/api/photos'
}

RoyalIcing avatar Jun 16 '17 09:06 RoyalIcing

Note the current approach may be fine!

export const load = async (props, prevProps) => {
  if (!prevProps) {
    return { items: await fetch('/api/photos').then(res => res.json()) }
  }
}

RoyalIcing avatar Jun 19 '17 23:06 RoyalIcing

Random thoughts after seeing this awesome framework:

I do similar things in https://github.com/LinasMatkasse/planck-state/blob/master/src/index.js#L16

There you have async actions called epics.

Each epic take one updater for loading state, one for success, and one for failure. In the future, the state will have some epic keys for loading and errors that will be set by the framework.

We follow jsonapi.org standards for error checking, so it's baked into the library. The error checker could be configurable in the future to allow different standards.

Also, a service is given that is supposed to be a promise. But since it comes from the outside, you can easily mock the service as a Promise.resolve(mockData) call.

Usage is as simple as calling normal action, and only the loading updater is exposed to the react component.

I did not yet solve some standard for initially loading something when constructing/mounting a new component, but that would mostly mean one extra updater function under a special key like loaders.

I have no clue if this helps or just confuses, but I'd really love to help a state management lib like this get popular to bring our community forward a bit :-)

Rant over! GLHF!

marcusradell avatar Jul 16 '17 21:07 marcusradell