项目作者: hydux

项目描述 :
Hydux + React = :heart:
高级语言: TypeScript
项目地址: git://github.com/hydux/hydux-react.git
创建时间: 2017-12-08T13:51:36Z
项目社区:https://github.com/hydux/hydux-react

开源协议:MIT License

下载


hydux-react

React renderer for hydux.

Install

  1. yarn add hydux hydux-react # or npm i hydux hydux-react

Usage

  1. import _app from 'hydux'
  2. import withPersist from 'hydux/lib/enhancers/persist'
  3. import withReact, { React } from 'hydux-react'
  4. let app = withReact()(_app)
  5. export default app({
  6. init: () => { count: 1 },
  7. actions: {
  8. down: () => state => ({ count: state.count - 1 }),
  9. up: () => state => ({ count: state.count + 1 })
  10. },
  11. view: (state: State, actions: Actions) =>
  12. <div>
  13. <h1>{state.count}</h1>
  14. <button onClick={actions.down}>–</button>
  15. <button onClick={actions.up}>+</button>
  16. </div>
  17. })

Helpers

PureView

React.PureComponent helper, with which the component uses a shallow prop comparison to determine whether to re-render, which in turn prevents unnecessary re-rendering.

  1. import { PureView } from 'hydux-react'
  2. export function View(props) {
  3. return (
  4. <PureView stateInUse={props}> // The props passed to PureView would be shallow compared and determine whether diff and render child components.
  5. {(props) => <ChildComponent {...props} ></ChildComponent>} // here we pass function as children to avoid executing child components' render function.
  6. </PureView>
  7. )
  8. }

ErrorBoundary

We can use the ErrorBoundary component to catch children’s error, which requires a React 16 feature.

  1. import { ErrorBoundary } from 'hydux-react'
  2. export function View(props) {
  3. return (
  4. <ErrorBoundary
  5. report={(error, errorInfo) => void} // Custom error handler function
  6. renderMessage={(error, errorInfo) => React.ChildNode} // Custom error message renderer, default is `return null`
  7. >
  8. {() => <ChildComponent {...props} ></ChildComponent>} // here we pass function as children to catch errors in child components' render function.
  9. </ErrorBoundary>
  10. )
  11. }

Counter App

  1. git clone https://github.com/hydux/hydux-react.git
  2. cd examples/counter
  3. yarn # or npm i
  4. npm start

Now open http://localhost:8080 and hack!

License

MIT