Git Product home page Git Product logo

reaclette's People

Contributors

ghemid-mohamed avatar julien-f avatar mathieura avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

reaclette's Issues

async computed?

const CitySelector = provideState({
  computed: {
    // it simply returns a promise instead of a value
    //
    // state.cities in the render before the promise settles
    //
    // if the promises rejects, a warning is displayed in
    // the console and the value will stay undefined
    cities: ({ country }) => loadCities(country)
  }
})(
  injectState(({ onChange, state, effects, value }) => (
    <select onChange={onChange} value={value}>
      {state.cities !== undefined
        ? state.cites.map(city => <option>{city}</option>)
        : null}
    </select>
  ))
)

Questions?

  • behavior if the factory is called again before the previous promise settle
  • behavior if the component is unmounted before the promise settle
  • should it possible to provide a default value instead of undefined?

Writable for computeds

It could be interesting to have settable computed:

const withState = provideState({
  initialState: () => ({ kelvin: 42 }),
  computeds: {
    celsius: {
      get: state => state.kelvin - 273.15,
      set (value) {
        state.kelvin = value + 273.15
      },
    },
  },
})

Other libs providing this feature:

[computed] Support async iterators

const Clock = provideState({
  computed: {
    // it simply returns an async iterator instead of a value
    //
    // the computed is undefined before the first
    // value is emitted by the iterator
    //
    // each new emitted value will trigger a new render with the
    // computed set to this value
    //
    // rejections are not handled and will possibly trigger an
    // unhandledRejection event on the window object, the computed will
    // keep its previous value
    //
    // this computed, which does not have dependencies, yields a new
    // date value every second
    async *date() {
      while (true) {
        yield new Date()
        await new Promise(resolve => setTimeout(resolve, 1e3))
      }
    }
  }
})(injectState(({ state }) => <p>{state.date.toISOString()}</p>))

`initialize` happen in child before parent

The problem comes from the fact that componentDidMount is executed first in children then in parents.

Main issue: the child state can use the parent state before it's initialized.

Derived issue: the parent state can be updated (due to a child) before the parent injectState has subscribed to it, therefore the parent component will not be re-rendered.

Possible solution: if the child state has a parent, defer the call to initialize after the one of the parent.

Find a name for this library

It was initial API compatible with Freactal but it's starting to take a shape on it's own, a new name should reflect that ๐Ÿ™‚

Propositions welcome!

Placeholders for async computed

This avoid testing and special cases for users (render, other computed or effects):

export default withState({
  computeds: {
    items: {
      get: () => fetch('/items').then(res => res.json()),
      placeholder: [],
    },
  },
}, ({ state }) => (
  <ul>
    {state.items.map(item => <li>{item}</li>)}
  </ul>
))

Without a placeholder, state.items would have been undefined before the items were fetched, it would have required special handling in the render to avoid an exception.

Major API improvement

Non-breaking changes:

  • provide a way to reset the state (#2)
  • default effects (#5)

Small breaking changes:

  • remove effects as injected arg in effects
  • remove reducer API (ability to return new state) in favor of this.state

Big breaking changes:

  • explicit inheritance (#12)

Existing APIs to check out:

Support dynamic values

const ExchangeBillboard = provideState({
  dynamic: {
    rate: props => /* returns an promise/async iterator/observable */,
  },
})(
  injectState(({ state }) =>
    <p>EUR/USD {state.rate}</p>
  )
)

Question: How can it be combined with computeds?

Improve inheritance model

Issue with current API:

  • implicit state/effects dependencies
  • a component placed between two components sharing state/effects will shadow them
  • cumbersome local use: provideState(injectState(Component))

I think we should consider departing completely from the implicit inheritance API and favor a local (with explicit inheritance) API.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.