首页 星云 工具 资源 星选 资讯 热门工具
:

PDF转图片 完全免费 小红书视频下载 无水印 抖音视频下载 无水印 数字星空

redux-0.8.0.zip

前端 18.16KB 12 需要积分: 1
立即下载

资源介绍:

一个可预测的全局状态管理的 JS 库 A JS library for predictable global state management
redux ========================= An experiment in fully hot-reloadable Flux. **The API might change any day.** _**Don't use in production.**_ ## Why another Flux framework? Read **[The Evolution of Flux Frameworks](https://medium.com/@dan_abramov/the-evolution-of-flux-frameworks-6c16ad26bb31)** for some context. ### Design Goals * Hot reloading of everything. * A hook for the future devtools to "commit" a state, and replay actions on top of it during hot reload. * No wrapper calls in your stores and actions. Your stuff is your stuff. * Super easy to test things in isolation without mocks. * I don't mind action constants. Seriously. * Keep Flux lingo. No cursors or observables in core. * Have I mentioned hot reloading yet? ## Demo ``` git clone https://github.com/gaearon/redux.git redux cd redux npm install npm start ``` ## What's it look like? ### Actions ```js // Still using constants... import { INCREMENT_COUNTER, DECREMENT_COUNTER } from '../constants/ActionTypes'; // But action creators are pure functions returning actions export function increment() { return { type: INCREMENT_COUNTER }; } export function decrement() { return { type: DECREMENT_COUNTER }; } // Can also be async if you return a function export function incrementAsync() { return perform => { setTimeout(() => { // Yay! Can invoke sync or async actions with `perform` perform(increment()); }, 1000); }; } // Could also read state of a store in the callback form export function incrementIfOdd() { return (perform, { counter }) => { if (counter % 2 === 0) { return; } perform(increment()); }; } ``` ### Stores ```js // ... too, use constants import { INCREMENT_COUNTER, DECREMENT_COUNTER } from '../constants/ActionTypes'; // what's important is that Store is a pure function, // and you can write it anyhow you like. // the Store signature is (state, action) => state, // and the state shape is up to you: you can use primitives, // objects, arrays, or even ImmutableJS objects. export default function counter(state = 0, action) { // this function returns the new state when an action comes switch (action.type) { case INCREMENT_COUNTER: return state + 1; case DECREMENT_COUNTER: return state - 1; default: return state; } // BUT THAT'S A SWITCH STATEMENT! // Right. If you hate 'em, see the FAQ below. } ``` ### Components #### Dumb Components ```js // The dumb component receives everything using props: import React, { PropTypes } from 'react'; export default class Counter { static propTypes = { increment: PropTypes.func.isRequired, decrement: PropTypes.func.isRequired, counter: PropTypes.number.isRequired }; render() { const { increment, decrement, counter } = this.props; return (

Clicked: {counter} times {' '} {' '}

); } } ``` #### Smart Components ```js // The smart component may observe stores using ``, // and bind actions to the dispatcher with `bindActions`. import React from 'react'; import { Connector, bindActions } from 'redux'; import Counter from '../components/Counter'; import * as CounterActions from '../actions/CounterActions'; // You can optionally specify `select` for finer-grained subscriptions // and retrieval. Only when the return value is shallowly different, // will the child component be updated. function select(state) { return { counter: state.counter }; } export default class CounterApp { render() { return ( {({ counter, dispatcher }) => /* Yes this is child as a function. */ } ); } } ``` #### Decorators The `@connect` decorator lets you create smart components less verbosely: ```js import React from 'react'; import { connect, bindActions } from 'redux'; import Counter from '../components/Counter'; import * as CounterActions from '../actions/CounterActions'; @connect(state => ({ counter: state.counter })) export default class CounterApp { render() { const { counter, dispatcher } = this.props; return ( ); } } ``` #### The root component Decorate your top-level component with `@provider(dispatcher)` (or `` inside) to bind it to a Redux dispatcher instance. Redux dispatcher accepts a single Store as an argument. Usually Flux apps have many Stores, so Redux provides a `composeStore` method that turns an object with Store functions as values (such as what you'd get from `import * as stores`) into a Store that [composes](https://gist.github.com/gaearon/d77ca812015c0356654f) them. Think `composeStores` is a “higher-order” Store because it creates a Store from several Stores. (You don't have to use it! You can just pass your own top-level Store function if that's what you prefer.) ```js import React from 'react'; import { createDispatcher, Provider, composeStores } from 'redux'; import CounterApp from './CounterApp'; import TodoApp from './TodoApp'; import * as stores from '../stores/index'; const dispatcher = createDispatcher(composeStores(stores)); export default class App { render() { return ( {() => /* Yep, function as a child. */
}
); } } ``` ## FAQ ### How does hot reloading work? * http://webpack.github.io/docs/hot-module-replacement.html * http://gaearon.github.io/react-hot-loader/ * Literally that's it. Redux is fully driven by component props, so it works on top of React Hot Loader. ### Can I use this in production? I wouldn't. Many use cases are not be considered yet. If you find some use cases this lib can't handle yet, please file an issue. ### But there are switch statements! `(state, action) => state` is as simple as a Store can get. You are free to implement your own `createStore`: ```js export default function createStore(initialState, handlers) { return (state = initialState, action) => handlers[action.type] ? handlers[action.type](state, action) : state; } ``` and use it for your Stores: ```js export default createStore(0, { [INCREMENT_COUNTER]: x => x + 1, [DECREMENT_COUNTER]: x => x - 1 }); ``` It's all just functions. Fancy stuff like generating stores from handler maps, or generating action creator constants, should be in userland. Redux has no opinion on how you do this in your project. ### What about `waitFor`? I wrote a lot of vanilla Flux code, and my only use case for it was avoiding emitting a change before a related Store consumes the action. In Redux this doesn't matter because the change is only emitted after *all* Stores have consumed the action. If several of your Stores want to read data from each other and depend on each other, it's a sign they should've been a single Store instead. [See this discussion on how `waitFor` can be replaced by the composition of stateless Stores.](https://gist.github.com/gaearon/d77ca812015c0356654f)

资源文件列表:

redux-0.8.0.zip 大约有49个文件
  1. redux-0.8.0/
  2. redux-0.8.0/.babelrc 43B
  3. redux-0.8.0/.eslintrc 349B
  4. redux-0.8.0/.gitignore 40B
  5. redux-0.8.0/.jshintrc 75B
  6. redux-0.8.0/.npmignore 13B
  7. redux-0.8.0/LICENSE 1.05KB
  8. redux-0.8.0/README.md 7.4KB
  9. redux-0.8.0/TODO 224B
  10. redux-0.8.0/examples/
  11. redux-0.8.0/examples/actions/
  12. redux-0.8.0/examples/actions/CounterActions.js 521B
  13. redux-0.8.0/examples/actions/TodoActions.js 133B
  14. redux-0.8.0/examples/components/
  15. redux-0.8.0/examples/components/AddTodo.js 327B
  16. redux-0.8.0/examples/components/Counter.js 501B
  17. redux-0.8.0/examples/components/TodoList.js 302B
  18. redux-0.8.0/examples/constants/
  19. redux-0.8.0/examples/constants/ActionTypes.js 145B
  20. redux-0.8.0/examples/containers/
  21. redux-0.8.0/examples/containers/App.js 505B
  22. redux-0.8.0/examples/containers/CounterApp.js 447B
  23. redux-0.8.0/examples/containers/TodoApp.js 628B
  24. redux-0.8.0/examples/index.html 157B
  25. redux-0.8.0/examples/index.js 126B
  26. redux-0.8.0/examples/server.js 420B
  27. redux-0.8.0/examples/stores/
  28. redux-0.8.0/examples/stores/counter.js 291B
  29. redux-0.8.0/examples/stores/index.js 62B
  30. redux-0.8.0/examples/stores/todos.js 325B
  31. redux-0.8.0/examples/webpack.config.js 742B
  32. redux-0.8.0/package.json 1.31KB
  33. redux-0.8.0/scripts/
  34. redux-0.8.0/scripts/build 65B
  35. redux-0.8.0/src/
  36. redux-0.8.0/src/Dispatcher.js 1.1KB
  37. redux-0.8.0/src/components/
  38. redux-0.8.0/src/components/Connector.js 1.47KB
  39. redux-0.8.0/src/components/Provider.js 977B
  40. redux-0.8.0/src/components/connect.js 802B
  41. redux-0.8.0/src/components/provide.js 468B
  42. redux-0.8.0/src/createDispatcher.js 305B
  43. redux-0.8.0/src/index.js 417B
  44. redux-0.8.0/src/utils/
  45. redux-0.8.0/src/utils/bindActions.js 235B
  46. redux-0.8.0/src/utils/composeStores.js 239B
  47. redux-0.8.0/src/utils/getDisplayName.js 119B
  48. redux-0.8.0/src/utils/shallowEqual.js 606B
  49. redux-0.8.0/src/utils/shallowEqualScalar.js 747B
0评论
提交 加载更多评论
其他资源 redux-0.9.0.zip
一个可预测的全局状态管理的 JS 库 A JS library for predictable global state management
redux-0.10.1.zip
一个可预测的全局状态管理的 JS 库 A JS library for predictable global state management
redux-0.11.0.zip
一个可预测的全局状态管理的 JS 库 A JS library for predictable global state management
redux-0.12.0.zip
一个可预测的全局状态管理的 JS 库 A JS library for predictable global state management
redux-0.8.1.zip
一个可预测的全局状态管理的 JS 库 A JS library for predictable global state management
redux-0.8.2.zip
一个可预测的全局状态管理的 JS 库 A JS library for predictable global state management
redux-0.10.0.zip
一个可预测的全局状态管理的 JS 库 A JS library for predictable global state management
redux-0.7.0.zip
一个可预测的全局状态管理的 JS 库 A JS library for predictable global state management