scoped-model

Contextual state management for React, ReasonReact and Preact

MIT License

Downloads
284
Stars
23
Committers
2

Bot releases are visible (Hide)

scoped-model - Props for models

Published by lxsmnsyc almost 5 years ago

  • Props can now be supplied to models through .Provider and be received by the model hook itself.

Hook:

const Counter = createModel(function ({ initialCount }) {
  const [count, setCount] = useState(initialCount);

  const increment = useCallback(() => {
    setCount(c => c + 1);
  }, []);

  const decrement = useCallback(() => {
    setCount(c => c - 1);
  }, []);

  return {
    count,
    increment,
    decrement,
  };
});

on Provider:

export default function App() {
  return (
    <Counter.Provider initialCount={1}>
      <Counter.Provider initialCount={0}>
        <Count />
        <Increment />
        <Decrement />
      </Counter.Provider>
      <Count />
      <IncDec />
    </Counter.Provider>
  );
}
scoped-model -

Published by lxsmnsyc almost 5 years ago

  • added useSelector and useSelectors, hooks that allows to transform the model's state before listening to the value.
scoped-model - Feature: useProperties

Published by lxsmnsyc almost 5 years ago

  • Added useProperties which allows users to listen to the changes of multiple properties.
function IncDec() {
  const [increment, decrement] = Counter.useProperties(['increment', 'decrement']);
  return (
    <React.Fragment>
      <button type="button" onClick={increment}>Increment</button>
      <button type="button" onClick={decrement}>Decrement</button>
    </React.Fragment>
  );
}
scoped-model -

Published by lxsmnsyc almost 5 years ago

  • Fine-grained property accesses
  • Shape-less models (removed the conventional model shape before.)
  • Leveraged Context API's observedBits dependency.
  • Replaced useModelState and useModelAction with useProperty.