RoyalIcing / react-organism

Dead simple React state management to bring pure components alive
https://react-organism.now.sh/
MIT License
223 stars 3 forks source link

Load from URL easily #4

Open RoyalIcing opened 7 years ago

RoyalIcing commented 7 years ago
export const load = '/api/photos'
RoyalIcing commented 7 years ago
export const load = {
  items: '/api/photos'
}
RoyalIcing commented 7 years ago

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()) }
  }
}
marcusradell commented 7 years ago

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!